blob: 4d0d86f14c4cfd1eae3ddc16c8402330b56af5e8 [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 Tarreaubaaee002006-06-26 02:48:02 +020039#include <proto/proto_http.h>
Willy Tarreaua8f55d52010-05-31 17:44:19 +020040#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010042#include <proto/server.h>
Willy Tarreau7c669d72008-06-20 15:04:11 +020043#include <proto/session.h>
Willy Tarreau9e000c62011-03-10 14:03:36 +010044#include <proto/stream_interface.h>
Willy Tarreaua8f55d52010-05-31 17:44:19 +020045#include <proto/stream_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020046#include <proto/task.h>
47
Willy Tarreaubaaee002006-06-26 02:48:02 +020048/*
49 * This function recounts the number of usable active and backup servers for
50 * proxy <p>. These numbers are returned into the p->srv_act and p->srv_bck.
Willy Tarreaub625a082007-11-26 01:15:43 +010051 * This function also recomputes the total active and backup weights. However,
Willy Tarreauf4cca452008-03-08 21:42:54 +010052 * it does not update tot_weight nor tot_used. Use update_backend_weight() for
Willy Tarreaub625a082007-11-26 01:15:43 +010053 * this.
Willy Tarreaubaaee002006-06-26 02:48:02 +020054 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +020055void recount_servers(struct proxy *px)
Willy Tarreaubaaee002006-06-26 02:48:02 +020056{
57 struct server *srv;
58
Willy Tarreau20697042007-11-15 23:26:18 +010059 px->srv_act = px->srv_bck = 0;
60 px->lbprm.tot_wact = px->lbprm.tot_wbck = 0;
Willy Tarreaub625a082007-11-26 01:15:43 +010061 px->lbprm.fbck = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +020062 for (srv = px->srv; srv != NULL; srv = srv->next) {
Willy Tarreaub625a082007-11-26 01:15:43 +010063 if (!srv_is_usable(srv->state, srv->eweight))
64 continue;
65
66 if (srv->state & SRV_BACKUP) {
67 if (!px->srv_bck &&
Willy Tarreauf4cca452008-03-08 21:42:54 +010068 !(px->options & PR_O_USE_ALL_BK))
Willy Tarreaub625a082007-11-26 01:15:43 +010069 px->lbprm.fbck = srv;
70 px->srv_bck++;
71 px->lbprm.tot_wbck += srv->eweight;
72 } else {
73 px->srv_act++;
74 px->lbprm.tot_wact += srv->eweight;
Willy Tarreaubaaee002006-06-26 02:48:02 +020075 }
76 }
Willy Tarreaub625a082007-11-26 01:15:43 +010077}
Willy Tarreau20697042007-11-15 23:26:18 +010078
Willy Tarreaub625a082007-11-26 01:15:43 +010079/* This function simply updates the backend's tot_weight and tot_used values
80 * after servers weights have been updated. It is designed to be used after
81 * recount_servers() or equivalent.
82 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +020083void update_backend_weight(struct proxy *px)
Willy Tarreaub625a082007-11-26 01:15:43 +010084{
Willy Tarreau20697042007-11-15 23:26:18 +010085 if (px->srv_act) {
86 px->lbprm.tot_weight = px->lbprm.tot_wact;
87 px->lbprm.tot_used = px->srv_act;
88 }
Willy Tarreaub625a082007-11-26 01:15:43 +010089 else if (px->lbprm.fbck) {
90 /* use only the first backup server */
91 px->lbprm.tot_weight = px->lbprm.fbck->eweight;
92 px->lbprm.tot_used = 1;
Willy Tarreau20697042007-11-15 23:26:18 +010093 }
94 else {
Willy Tarreaub625a082007-11-26 01:15:43 +010095 px->lbprm.tot_weight = px->lbprm.tot_wbck;
96 px->lbprm.tot_used = px->srv_bck;
Willy Tarreau20697042007-11-15 23:26:18 +010097 }
Willy Tarreaub625a082007-11-26 01:15:43 +010098}
99
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200100/*
101 * This function tries to find a running server for the proxy <px> following
102 * the source hash method. Depending on the number of active/backup servers,
103 * it will either look for active servers, or for backup servers.
104 * If any server is found, it will be returned. If no valid server is found,
105 * NULL is returned.
106 */
107struct server *get_server_sh(struct proxy *px, const char *addr, int len)
108{
109 unsigned int h, l;
110
111 if (px->lbprm.tot_weight == 0)
112 return NULL;
113
114 l = h = 0;
115
116 /* note: we won't hash if there's only one server left */
117 if (px->lbprm.tot_used == 1)
118 goto hash_done;
119
120 while ((l + sizeof (int)) <= len) {
121 h ^= ntohl(*(unsigned int *)(&addr[l]));
122 l += sizeof (int);
123 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100124 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
125 h = full_hash(h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200126 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200127 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
128 return chash_get_server_hash(px, h);
129 else
130 return map_get_server_hash(px, h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200131}
132
133/*
134 * This function tries to find a running server for the proxy <px> following
135 * the URI hash method. In order to optimize cache hits, the hash computation
136 * ends at the question mark. Depending on the number of active/backup servers,
137 * it will either look for active servers, or for backup servers.
138 * If any server is found, it will be returned. If no valid server is found,
139 * NULL is returned.
140 *
141 * This code was contributed by Guillaume Dallaire, who also selected this hash
142 * algorithm out of a tens because it gave him the best results.
143 *
144 */
145struct server *get_server_uh(struct proxy *px, char *uri, int uri_len)
146{
147 unsigned long hash = 0;
148 int c;
149 int slashes = 0;
150
151 if (px->lbprm.tot_weight == 0)
152 return NULL;
153
154 /* note: we won't hash if there's only one server left */
155 if (px->lbprm.tot_used == 1)
156 goto hash_done;
157
158 if (px->uri_len_limit)
159 uri_len = MIN(uri_len, px->uri_len_limit);
160
161 while (uri_len--) {
162 c = *uri++;
163 if (c == '/') {
164 slashes++;
165 if (slashes == px->uri_dirs_depth1) /* depth+1 */
166 break;
167 }
168 else if (c == '?')
169 break;
170
171 hash = c + (hash << 6) + (hash << 16) - hash;
172 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100173 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
174 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200175 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200176 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
177 return chash_get_server_hash(px, hash);
178 else
179 return map_get_server_hash(px, hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200180}
181
Willy Tarreau01732802007-11-01 22:48:15 +0100182/*
183 * This function tries to find a running server for the proxy <px> following
184 * the URL parameter hash method. It looks for a specific parameter in the
185 * URL and hashes it to compute the server ID. This is useful to optimize
186 * performance by avoiding bounces between servers in contexts where sessions
187 * are shared but cookies are not usable. If the parameter is not found, NULL
188 * is returned. If any server is found, it will be returned. If no valid server
189 * is found, NULL is returned.
Willy Tarreau01732802007-11-01 22:48:15 +0100190 */
191struct server *get_server_ph(struct proxy *px, const char *uri, int uri_len)
192{
193 unsigned long hash = 0;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200194 const char *p;
195 const char *params;
Willy Tarreau01732802007-11-01 22:48:15 +0100196 int plen;
197
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200198 /* when tot_weight is 0 then so is srv_count */
Willy Tarreau20697042007-11-15 23:26:18 +0100199 if (px->lbprm.tot_weight == 0)
Willy Tarreau01732802007-11-01 22:48:15 +0100200 return NULL;
201
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200202 if ((p = memchr(uri, '?', uri_len)) == NULL)
203 return NULL;
204
Willy Tarreau01732802007-11-01 22:48:15 +0100205 p++;
206
207 uri_len -= (p - uri);
208 plen = px->url_param_len;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200209 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100210
211 while (uri_len > plen) {
212 /* Look for the parameter name followed by an equal symbol */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200213 if (params[plen] == '=') {
214 if (memcmp(params, px->url_param_name, plen) == 0) {
215 /* OK, we have the parameter here at <params>, and
Willy Tarreau01732802007-11-01 22:48:15 +0100216 * the value after the equal sign, at <p>
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200217 * skip the equal symbol
Willy Tarreau01732802007-11-01 22:48:15 +0100218 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200219 p += plen + 1;
220 uri_len -= plen + 1;
221
Willy Tarreau01732802007-11-01 22:48:15 +0100222 while (uri_len && *p != '&') {
223 hash = *p + (hash << 6) + (hash << 16) - hash;
224 uri_len--;
225 p++;
226 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100227 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
228 hash = full_hash(hash);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200229 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
230 return chash_get_server_hash(px, hash);
231 else
232 return map_get_server_hash(px, hash);
Willy Tarreau01732802007-11-01 22:48:15 +0100233 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200234 }
235 /* skip to next parameter */
236 p = memchr(params, '&', uri_len);
237 if (!p)
238 return NULL;
239 p++;
240 uri_len -= (p - params);
241 params = p;
242 }
243 return NULL;
244}
245
246/*
247 * this does the same as the previous server_ph, but check the body contents
248 */
249struct server *get_server_ph_post(struct session *s)
250{
251 unsigned long hash = 0;
252 struct http_txn *txn = &s->txn;
253 struct buffer *req = s->req;
254 struct http_msg *msg = &txn->req;
255 struct proxy *px = s->be;
256 unsigned int plen = px->url_param_len;
Willy Tarreau124d9912011-03-01 20:30:48 +0100257 unsigned long len = msg->body_len;
Willy Tarreauea1175a2012-03-05 15:52:30 +0100258 const char *params = req->p + msg->sov;
Willy Tarreau157dd632009-12-06 19:18:09 +0100259 const char *p = params;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200260
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100261 if (len > req->i - (msg->sov - msg->som))
262 len = req->i - (msg->sov - msg->som);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200263
Willy Tarreau157dd632009-12-06 19:18:09 +0100264 if (len == 0)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200265 return NULL;
266
Willy Tarreau157dd632009-12-06 19:18:09 +0100267 if (px->lbprm.tot_weight == 0)
268 return NULL;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200269
270 while (len > plen) {
271 /* Look for the parameter name followed by an equal symbol */
272 if (params[plen] == '=') {
273 if (memcmp(params, px->url_param_name, plen) == 0) {
274 /* OK, we have the parameter here at <params>, and
275 * the value after the equal sign, at <p>
276 * skip the equal symbol
277 */
278 p += plen + 1;
279 len -= plen + 1;
280
281 while (len && *p != '&') {
282 if (unlikely(!HTTP_IS_TOKEN(*p))) {
Willy Tarreau157dd632009-12-06 19:18:09 +0100283 /* if in a POST, body must be URI encoded or it's not a URI.
284 * Do not interprete any possible binary data as a parameter.
285 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200286 if (likely(HTTP_IS_LWS(*p))) /* eol, uncertain uri len */
287 break;
288 return NULL; /* oh, no; this is not uri-encoded.
289 * This body does not contain parameters.
290 */
291 }
292 hash = *p + (hash << 6) + (hash << 16) - hash;
293 len--;
294 p++;
295 /* should we break if vlen exceeds limit? */
296 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100297 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
298 hash = full_hash(hash);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200299 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
300 return chash_get_server_hash(px, hash);
301 else
302 return map_get_server_hash(px, hash);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200303 }
304 }
Willy Tarreau01732802007-11-01 22:48:15 +0100305 /* skip to next parameter */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200306 p = memchr(params, '&', len);
Willy Tarreau01732802007-11-01 22:48:15 +0100307 if (!p)
308 return NULL;
309 p++;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200310 len -= (p - params);
311 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100312 }
313 return NULL;
314}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200315
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200316
Willy Tarreaubaaee002006-06-26 02:48:02 +0200317/*
Benoitaffb4812009-03-25 13:02:10 +0100318 * This function tries to find a running server for the proxy <px> following
319 * the Header parameter hash method. It looks for a specific parameter in the
320 * URL and hashes it to compute the server ID. This is useful to optimize
321 * performance by avoiding bounces between servers in contexts where sessions
322 * are shared but cookies are not usable. If the parameter is not found, NULL
323 * is returned. If any server is found, it will be returned. If no valid server
324 * is found, NULL is returned.
325 */
326struct server *get_server_hh(struct session *s)
327{
328 unsigned long hash = 0;
329 struct http_txn *txn = &s->txn;
330 struct http_msg *msg = &txn->req;
331 struct proxy *px = s->be;
332 unsigned int plen = px->hh_len;
333 unsigned long len;
334 struct hdr_ctx ctx;
335 const char *p;
336
337 /* tot_weight appears to mean srv_count */
338 if (px->lbprm.tot_weight == 0)
339 return NULL;
340
Benoitaffb4812009-03-25 13:02:10 +0100341 ctx.idx = 0;
342
343 /* if the message is chunked, we skip the chunk size, but use the value as len */
Willy Tarreau3a215be2012-03-09 21:39:51 +0100344 http_find_header2(px->hh_name, plen, s->req->p + msg->sol, &txn->hdr_idx, &ctx);
Benoitaffb4812009-03-25 13:02:10 +0100345
346 /* if the header is not found or empty, let's fallback to round robin */
347 if (!ctx.idx || !ctx.vlen)
348 return NULL;
349
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200350 /* note: we won't hash if there's only one server left */
351 if (px->lbprm.tot_used == 1)
352 goto hash_done;
353
Benoitaffb4812009-03-25 13:02:10 +0100354 /* Found a the hh_name in the headers.
355 * we will compute the hash based on this value ctx.val.
356 */
357 len = ctx.vlen;
358 p = (char *)ctx.line + ctx.val;
359 if (!px->hh_match_domain) {
360 while (len) {
361 hash = *p + (hash << 6) + (hash << 16) - hash;
362 len--;
363 p++;
364 }
365 } else {
366 int dohash = 0;
367 p += len - 1;
368 /* special computation, use only main domain name, not tld/host
369 * going back from the end of string, start hashing at first
370 * dot stop at next.
371 * This is designed to work with the 'Host' header, and requires
372 * a special option to activate this.
373 */
374 while (len) {
375 if (*p == '.') {
376 if (!dohash)
377 dohash = 1;
378 else
379 break;
380 } else {
381 if (dohash)
382 hash = *p + (hash << 6) + (hash << 16) - hash;
383 }
384 len--;
385 p--;
386 }
387 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100388 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
389 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200390 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200391 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
392 return chash_get_server_hash(px, hash);
393 else
394 return map_get_server_hash(px, hash);
Benoitaffb4812009-03-25 13:02:10 +0100395}
396
Willy Tarreau34db1082012-04-19 17:16:54 +0200397/* RDP Cookie HASH. */
Emeric Brun736aa232009-06-30 17:56:00 +0200398struct server *get_server_rch(struct session *s)
399{
400 unsigned long hash = 0;
401 struct proxy *px = s->be;
402 unsigned long len;
403 const char *p;
404 int ret;
405 struct acl_expr expr;
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
Emeric Brun736aa232009-06-30 17:56:00 +0200413 memset(&expr, 0, sizeof(expr));
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
421 expr.args = args;
Emeric Brun736aa232009-06-30 17:56:00 +0200422
Willy Tarreau37406352012-04-23 16:16:37 +0200423 ret = acl_fetch_rdp_cookie(px, s, NULL, ACL_DIR_REQ, &expr, &smp);
Willy Tarreauf853c462012-04-23 18:53:56 +0200424 len = smp.data.str.len;
Willy Tarreau664092c2011-12-16 19:11:42 +0100425
Willy Tarreau37406352012-04-23 16:16:37 +0200426 if (ret == 0 || (smp.flags & SMP_F_MAY_CHANGE) || len == 0)
Emeric Brun736aa232009-06-30 17:56:00 +0200427 return NULL;
428
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200429 /* note: we won't hash if there's only one server left */
430 if (px->lbprm.tot_used == 1)
431 goto hash_done;
432
Emeric Brun736aa232009-06-30 17:56:00 +0200433 /* Found a the hh_name in the headers.
434 * we will compute the hash based on this value ctx.val.
435 */
Willy Tarreauf853c462012-04-23 18:53:56 +0200436 p = smp.data.str.str;
Emeric Brun736aa232009-06-30 17:56:00 +0200437 while (len) {
438 hash = *p + (hash << 6) + (hash << 16) - hash;
439 len--;
440 p++;
441 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100442 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
443 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200444 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200445 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
446 return chash_get_server_hash(px, hash);
447 else
448 return map_get_server_hash(px, hash);
Emeric Brun736aa232009-06-30 17:56:00 +0200449}
Benoitaffb4812009-03-25 13:02:10 +0100450
451/*
Willy Tarreau7c669d72008-06-20 15:04:11 +0200452 * This function applies the load-balancing algorithm to the session, as
453 * defined by the backend it is assigned to. The session is then marked as
454 * 'assigned'.
455 *
456 * This function MAY NOT be called with SN_ASSIGNED already set. If the session
457 * had a server previously assigned, it is rebalanced, trying to avoid the same
Willy Tarreau827aee92011-03-10 16:55:02 +0100458 * server, which should still be present in target_srv(&s->target) before the call.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200459 * The function tries to keep the original connection slot if it reconnects to
460 * the same server, otherwise it releases it and tries to offer it.
461 *
462 * It is illegal to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200463 *
464 * It may return :
Willy Tarreau664beb82011-03-10 11:38:29 +0100465 * SRV_STATUS_OK if everything is OK. ->srv and ->target are assigned.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200466 * SRV_STATUS_NOSRV if no server is available. Session is not ASSIGNED
467 * SRV_STATUS_FULL if all servers are saturated. Session is not ASSIGNED
Willy Tarreaubaaee002006-06-26 02:48:02 +0200468 * SRV_STATUS_INTERNAL for other unrecoverable errors.
469 *
Willy Tarreau7c669d72008-06-20 15:04:11 +0200470 * Upon successful return, the session flag SN_ASSIGNED is set to indicate that
Willy Tarreau827aee92011-03-10 16:55:02 +0100471 * it does not need to be called anymore. This means that target_srv(&s->target)
472 * can be trusted in balance and direct modes.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200473 *
474 */
475
476int assign_server(struct session *s)
477{
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100478
Willy Tarreau7c669d72008-06-20 15:04:11 +0200479 struct server *conn_slot;
Willy Tarreau827aee92011-03-10 16:55:02 +0100480 struct server *srv, *prev_srv;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200481 int err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100482
Simon Horman8effd3d2011-08-13 08:03:52 +0900483 DPRINTF(stderr,"assign_server : s=%p\n",s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200484
Willy Tarreau7c669d72008-06-20 15:04:11 +0200485 err = SRV_STATUS_INTERNAL;
486 if (unlikely(s->pend_pos || s->flags & SN_ASSIGNED))
487 goto out_err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100488
Willy Tarreau827aee92011-03-10 16:55:02 +0100489 prev_srv = target_srv(&s->target);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200490 conn_slot = s->srv_conn;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200491
Willy Tarreau7c669d72008-06-20 15:04:11 +0200492 /* We have to release any connection slot before applying any LB algo,
493 * otherwise we may erroneously end up with no available slot.
494 */
495 if (conn_slot)
496 sess_change_server(s, NULL);
497
Willy Tarreau827aee92011-03-10 16:55:02 +0100498 /* We will now try to find the good server and store it into <target_srv(&s->target)>.
499 * Note that <target_srv(&s->target)> may be NULL in case of dispatch or proxy mode,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200500 * as well as if no server is available (check error code).
501 */
Willy Tarreau1a20a5d2007-11-01 21:08:19 +0100502
Willy Tarreau827aee92011-03-10 16:55:02 +0100503 srv = NULL;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100504 clear_target(&s->target);
Willy Tarreau664beb82011-03-10 11:38:29 +0100505
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200506 if (s->be->lbprm.algo & BE_LB_KIND) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200507 /* we must check if we have at least one server available */
508 if (!s->be->lbprm.tot_weight) {
509 err = SRV_STATUS_NOSRV;
510 goto out;
511 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200512
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200513 /* First check whether we need to fetch some data or simply call
514 * the LB lookup function. Only the hashing functions will need
515 * some input data in fact, and will support multiple algorithms.
516 */
517 switch (s->be->lbprm.algo & BE_LB_LKUP) {
518 case BE_LB_LKUP_RRTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100519 srv = fwrr_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200520 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200521
Willy Tarreauf09c6602012-02-13 17:12:08 +0100522 case BE_LB_LKUP_FSTREE:
523 srv = fas_get_next_server(s->be, prev_srv);
524 break;
525
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200526 case BE_LB_LKUP_LCTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100527 srv = fwlc_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200528 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200529
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200530 case BE_LB_LKUP_CHTREE:
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200531 case BE_LB_LKUP_MAP:
Willy Tarreau9757a382009-10-03 12:56:50 +0200532 if ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200533 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100534 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200535 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100536 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau9757a382009-10-03 12:56:50 +0200537 break;
538 }
539 else if ((s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_HI) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200540 /* unknown balancing algorithm */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200541 err = SRV_STATUS_INTERNAL;
542 goto out;
543 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200544
545 switch (s->be->lbprm.algo & BE_LB_PARM) {
546 case BE_LB_HASH_SRC:
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200547 if (s->req->prod->addr.from.ss_family == AF_INET) {
548 srv = get_server_sh(s->be,
549 (void *)&((struct sockaddr_in *)&s->req->prod->addr.from)->sin_addr,
550 4);
551 }
552 else if (s->req->prod->addr.from.ss_family == AF_INET6) {
553 srv = get_server_sh(s->be,
554 (void *)&((struct sockaddr_in6 *)&s->req->prod->addr.from)->sin6_addr,
555 16);
556 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200557 else {
558 /* unknown IP family */
559 err = SRV_STATUS_INTERNAL;
560 goto out;
561 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200562 break;
563
564 case BE_LB_HASH_URI:
565 /* URI hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100566 if (s->txn.req.msg_state < HTTP_MSG_BODY)
567 break;
Willy Tarreau827aee92011-03-10 16:55:02 +0100568 srv = get_server_uh(s->be,
Willy Tarreau3a215be2012-03-09 21:39:51 +0100569 s->req->p + s->txn.req.sol + s->txn.req.sl.rq.u,
Willy Tarreau827aee92011-03-10 16:55:02 +0100570 s->txn.req.sl.rq.u_l);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200571 break;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200572
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200573 case BE_LB_HASH_PRM:
574 /* URL Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100575 if (s->txn.req.msg_state < HTTP_MSG_BODY)
576 break;
Willy Tarreau61a21a32011-03-01 20:35:49 +0100577
Willy Tarreau827aee92011-03-10 16:55:02 +0100578 srv = get_server_ph(s->be,
Willy Tarreau3a215be2012-03-09 21:39:51 +0100579 s->req->p + s->txn.req.sol + s->txn.req.sl.rq.u,
Willy Tarreau827aee92011-03-10 16:55:02 +0100580 s->txn.req.sl.rq.u_l);
Willy Tarreau61a21a32011-03-01 20:35:49 +0100581
Willy Tarreau827aee92011-03-10 16:55:02 +0100582 if (!srv && s->txn.meth == HTTP_METH_POST)
583 srv = get_server_ph_post(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200584 break;
Benoitaffb4812009-03-25 13:02:10 +0100585
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200586 case BE_LB_HASH_HDR:
587 /* Header Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100588 if (s->txn.req.msg_state < HTTP_MSG_BODY)
589 break;
Willy Tarreau827aee92011-03-10 16:55:02 +0100590 srv = get_server_hh(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200591 break;
592
593 case BE_LB_HASH_RDP:
594 /* RDP Cookie hashing */
Willy Tarreau827aee92011-03-10 16:55:02 +0100595 srv = get_server_rch(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200596 break;
597
598 default:
599 /* unknown balancing algorithm */
600 err = SRV_STATUS_INTERNAL;
601 goto out;
Benoitaffb4812009-03-25 13:02:10 +0100602 }
Emeric Brun736aa232009-06-30 17:56:00 +0200603
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200604 /* If the hashing parameter was not found, let's fall
605 * back to round robin on the map.
606 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100607 if (!srv) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200608 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100609 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200610 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100611 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200612 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200613
614 /* end of map-based LB */
Emeric Brun736aa232009-06-30 17:56:00 +0200615 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200616
Willy Tarreau7c669d72008-06-20 15:04:11 +0200617 default:
618 /* unknown balancing algorithm */
619 err = SRV_STATUS_INTERNAL;
620 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200621 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200622
Willy Tarreau827aee92011-03-10 16:55:02 +0100623 if (!srv) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200624 err = SRV_STATUS_FULL;
625 goto out;
626 }
Willy Tarreau827aee92011-03-10 16:55:02 +0100627 else if (srv != prev_srv) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100628 s->be->be_counters.cum_lbconn++;
Willy Tarreau827aee92011-03-10 16:55:02 +0100629 srv->counters.cum_lbconn++;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100630 }
Willy Tarreau827aee92011-03-10 16:55:02 +0100631 set_target_server(&s->target, srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200632 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200633 else if (s->be->options & (PR_O_DISPATCH | PR_O_TRANSP)) {
Willy Tarreau9e000c62011-03-10 14:03:36 +0100634 set_target_proxy(&s->target, s->be);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200635 }
David du Colombier6f5ccb12011-03-10 22:26:24 +0100636 else if ((s->be->options & PR_O_HTTP_PROXY) &&
Willy Tarreau6471afb2011-09-23 10:54:59 +0200637 is_addr(&s->req->cons->addr.to)) {
Willy Tarreau664beb82011-03-10 11:38:29 +0100638 /* in proxy mode, we need a valid destination address */
Willy Tarreau9e000c62011-03-10 14:03:36 +0100639 set_target_proxy(&s->target, s->be);
Willy Tarreau664beb82011-03-10 11:38:29 +0100640 }
641 else {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200642 err = SRV_STATUS_NOSRV;
643 goto out;
644 }
645
646 s->flags |= SN_ASSIGNED;
647 err = SRV_STATUS_OK;
648 out:
649
650 /* Either we take back our connection slot, or we offer it to someone
651 * else if we don't need it anymore.
652 */
653 if (conn_slot) {
Willy Tarreau827aee92011-03-10 16:55:02 +0100654 if (conn_slot == srv) {
655 sess_change_server(s, srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200656 } else {
657 if (may_dequeue_tasks(conn_slot, s->be))
658 process_srv_queue(conn_slot);
659 }
660 }
661
662 out_err:
663 return err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200664}
665
666
667/*
668 * This function assigns a server address to a session, and sets SN_ADDR_SET.
669 * The address is taken from the currently assigned server, or from the
670 * dispatch or transparent address.
671 *
672 * It may return :
673 * SRV_STATUS_OK if everything is OK.
674 * SRV_STATUS_INTERNAL for other unrecoverable errors.
675 *
676 * Upon successful return, the session flag SN_ADDR_SET is set. This flag is
677 * not cleared, so it's to the caller to clear it if required.
678 *
679 */
680int assign_server_address(struct session *s)
681{
682#ifdef DEBUG_FULL
683 fprintf(stderr,"assign_server_address : s=%p\n",s);
684#endif
685
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200686 if ((s->flags & SN_DIRECT) || (s->be->lbprm.algo & BE_LB_KIND)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200687 /* A server is necessarily known for this session */
688 if (!(s->flags & SN_ASSIGNED))
689 return SRV_STATUS_INTERNAL;
690
Willy Tarreau6471afb2011-09-23 10:54:59 +0200691 s->req->cons->addr.to = target_srv(&s->target)->addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200692
Willy Tarreau6471afb2011-09-23 10:54:59 +0200693 if (!is_addr(&s->req->cons->addr.to)) {
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200694 /* if the server has no address, we use the same address
695 * the client asked, which is handy for remapping ports
696 * locally on multiple addresses at once.
697 */
Willy Tarreau9b061e32012-04-07 18:03:52 +0200698 if (!(s->be->options & PR_O_TRANSP))
699 stream_sock_get_to_addr(s->req->prod);
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200700
Willy Tarreau6471afb2011-09-23 10:54:59 +0200701 if (s->req->prod->addr.to.ss_family == AF_INET) {
702 ((struct sockaddr_in *)&s->req->cons->addr.to)->sin_addr = ((struct sockaddr_in *)&s->req->prod->addr.to)->sin_addr;
703 } else if (s->req->prod->addr.to.ss_family == AF_INET6) {
704 ((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 +0200705 }
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200706 }
707
Willy Tarreaubaaee002006-06-26 02:48:02 +0200708 /* if this server remaps proxied ports, we'll use
709 * the port the client connected to with an offset. */
Willy Tarreau827aee92011-03-10 16:55:02 +0100710 if (target_srv(&s->target)->state & SRV_MAPPORTS) {
David du Colombier6f5ccb12011-03-10 22:26:24 +0100711 int base_port;
712
Willy Tarreau9b061e32012-04-07 18:03:52 +0200713 if (!(s->be->options & PR_O_TRANSP))
714 stream_sock_get_to_addr(s->req->prod);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100715
716 /* First, retrieve the port from the incoming connection */
Willy Tarreau6471afb2011-09-23 10:54:59 +0200717 base_port = get_host_port(&s->req->prod->addr.to);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100718
719 /* Second, assign the outgoing connection's port */
Willy Tarreau6471afb2011-09-23 10:54:59 +0200720 base_port += get_host_port(&s->req->cons->addr.to);
721 set_host_port(&s->req->cons->addr.to, base_port);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200722 }
723 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200724 else if (s->be->options & PR_O_DISPATCH) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200725 /* connect to the defined dispatch addr */
Willy Tarreau6471afb2011-09-23 10:54:59 +0200726 s->req->cons->addr.to = s->be->dispatch_addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200727 }
Willy Tarreau4b1f8592008-12-23 23:13:55 +0100728 else if (s->be->options & PR_O_TRANSP) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200729 /* in transparent mode, use the original dest addr if no dispatch specified */
Willy Tarreau9b061e32012-04-07 18:03:52 +0200730 stream_sock_get_to_addr(s->req->prod);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200731
Willy Tarreau6471afb2011-09-23 10:54:59 +0200732 if (s->req->prod->addr.to.ss_family == AF_INET || s->req->prod->addr.to.ss_family == AF_INET6) {
733 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 +0200734 }
Willy Tarreaubd414282008-01-19 13:46:35 +0100735 /* when we support IPv6 on the backend, we may add other tests */
736 //qfprintf(stderr, "Cannot get original server address.\n");
737 //return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200738 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100739 else if (s->be->options & PR_O_HTTP_PROXY) {
740 /* If HTTP PROXY option is set, then server is already assigned
741 * during incoming client request parsing. */
742 }
Willy Tarreau1a1158b2007-01-20 11:07:46 +0100743 else {
744 /* no server and no LB algorithm ! */
745 return SRV_STATUS_INTERNAL;
746 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200747
748 s->flags |= SN_ADDR_SET;
749 return SRV_STATUS_OK;
750}
751
752
753/* This function assigns a server to session <s> if required, and can add the
754 * connection to either the assigned server's queue or to the proxy's queue.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200755 * If ->srv_conn is set, the session is first released from the server.
756 * It may also be called with SN_DIRECT and/or SN_ASSIGNED though. It will
757 * be called before any connection and after any retry or redispatch occurs.
758 *
759 * It is not allowed to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200760 *
761 * Returns :
762 *
763 * SRV_STATUS_OK if everything is OK.
Willy Tarreau827aee92011-03-10 16:55:02 +0100764 * SRV_STATUS_NOSRV if no server is available. target_srv(&s->target) = NULL.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200765 * SRV_STATUS_QUEUED if the connection has been queued.
766 * SRV_STATUS_FULL if the server(s) is/are saturated and the
Willy Tarreau827aee92011-03-10 16:55:02 +0100767 * connection could not be queued at the server's,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200768 * which may be NULL if we queue on the backend.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200769 * SRV_STATUS_INTERNAL for other unrecoverable errors.
770 *
771 */
772int assign_server_and_queue(struct session *s)
773{
774 struct pendconn *p;
Willy Tarreau827aee92011-03-10 16:55:02 +0100775 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200776 int err;
777
778 if (s->pend_pos)
779 return SRV_STATUS_INTERNAL;
780
Willy Tarreau7c669d72008-06-20 15:04:11 +0200781 err = SRV_STATUS_OK;
782 if (!(s->flags & SN_ASSIGNED)) {
Willy Tarreau827aee92011-03-10 16:55:02 +0100783 struct server *prev_srv = target_srv(&s->target);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100784
Willy Tarreau7c669d72008-06-20 15:04:11 +0200785 err = assign_server(s);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100786 if (prev_srv) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200787 /* This session was previously assigned to a server. We have to
788 * update the session's and the server's stats :
789 * - if the server changed :
790 * - set TX_CK_DOWN if txn.flags was TX_CK_VALID
791 * - set SN_REDISP if it was successfully redispatched
792 * - increment srv->redispatches and be->redispatches
793 * - if the server remained the same : update retries.
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100794 */
795
Willy Tarreau827aee92011-03-10 16:55:02 +0100796 if (prev_srv != target_srv(&s->target)) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200797 if ((s->txn.flags & TX_CK_MASK) == TX_CK_VALID) {
798 s->txn.flags &= ~TX_CK_MASK;
799 s->txn.flags |= TX_CK_DOWN;
800 }
801 s->flags |= SN_REDISP;
Willy Tarreau3d80d912011-03-10 11:42:13 +0100802 prev_srv->counters.redispatches++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100803 s->be->be_counters.redispatches++;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200804 } else {
Willy Tarreau3d80d912011-03-10 11:42:13 +0100805 prev_srv->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100806 s->be->be_counters.retries++;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100807 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100808 }
809 }
810
Willy Tarreaubaaee002006-06-26 02:48:02 +0200811 switch (err) {
812 case SRV_STATUS_OK:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200813 /* we have SN_ASSIGNED set */
Willy Tarreau827aee92011-03-10 16:55:02 +0100814 srv = target_srv(&s->target);
815 if (!srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200816 return SRV_STATUS_OK; /* dispatch or proxy mode */
817
818 /* If we already have a connection slot, no need to check any queue */
Willy Tarreau827aee92011-03-10 16:55:02 +0100819 if (s->srv_conn == srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200820 return SRV_STATUS_OK;
821
822 /* OK, this session already has an assigned server, but no
823 * connection slot yet. Either it is a redispatch, or it was
824 * assigned from persistence information (direct mode).
825 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100826 if ((s->flags & SN_REDIRECTABLE) && srv->rdr_len) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200827 /* server scheduled for redirection, and already assigned. We
828 * don't want to go further nor check the queue.
Willy Tarreau21d2af32008-02-14 20:25:24 +0100829 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100830 sess_change_server(s, srv); /* not really needed in fact */
Willy Tarreau21d2af32008-02-14 20:25:24 +0100831 return SRV_STATUS_OK;
832 }
833
Willy Tarreau7c669d72008-06-20 15:04:11 +0200834 /* We might have to queue this session if the assigned server is full.
835 * We know we have to queue it into the server's queue, so if a maxqueue
836 * is set on the server, we must also check that the server's queue is
837 * not full, in which case we have to return FULL.
838 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100839 if (srv->maxconn &&
840 (srv->nbpend || srv->served >= srv_dynamic_maxconn(srv))) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200841
Willy Tarreau827aee92011-03-10 16:55:02 +0100842 if (srv->maxqueue > 0 && srv->nbpend >= srv->maxqueue)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200843 return SRV_STATUS_FULL;
844
Willy Tarreaubaaee002006-06-26 02:48:02 +0200845 p = pendconn_add(s);
846 if (p)
847 return SRV_STATUS_QUEUED;
848 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200849 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200850 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200851
852 /* OK, we can use this server. Let's reserve our place */
Willy Tarreau827aee92011-03-10 16:55:02 +0100853 sess_change_server(s, srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200854 return SRV_STATUS_OK;
855
856 case SRV_STATUS_FULL:
857 /* queue this session into the proxy's queue */
858 p = pendconn_add(s);
859 if (p)
860 return SRV_STATUS_QUEUED;
861 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200862 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200863
864 case SRV_STATUS_NOSRV:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200865 return err;
866
Willy Tarreaubaaee002006-06-26 02:48:02 +0200867 case SRV_STATUS_INTERNAL:
868 return err;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200869
Willy Tarreaubaaee002006-06-26 02:48:02 +0200870 default:
871 return SRV_STATUS_INTERNAL;
872 }
Willy Tarreau5b6995c2008-01-13 16:31:17 +0100873}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200874
Willy Tarreaub1d67742010-03-29 19:36:59 +0200875/* If an explicit source binding is specified on the server and/or backend, and
876 * this source makes use of the transparent proxy, then it is extracted now and
Willy Tarreau6471afb2011-09-23 10:54:59 +0200877 * assigned to the session's req->cons->addr.from entry.
Willy Tarreaub1d67742010-03-29 19:36:59 +0200878 */
879static void assign_tproxy_address(struct session *s)
880{
881#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY)
Willy Tarreau827aee92011-03-10 16:55:02 +0100882 struct server *srv = target_srv(&s->target);
883
884 if (srv && srv->state & SRV_BIND_SRC) {
885 switch (srv->state & SRV_TPROXY_MASK) {
Willy Tarreaub1d67742010-03-29 19:36:59 +0200886 case SRV_TPROXY_ADDR:
Willy Tarreau6471afb2011-09-23 10:54:59 +0200887 s->req->cons->addr.from = srv->tproxy_addr;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200888 break;
889 case SRV_TPROXY_CLI:
890 case SRV_TPROXY_CIP:
Emeric Brunec810d12010-10-22 16:36:33 +0200891 /* FIXME: what can we do if the client connects in IPv6 or unix socket ? */
Willy Tarreau6471afb2011-09-23 10:54:59 +0200892 s->req->cons->addr.from = s->req->prod->addr.from;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200893 break;
Willy Tarreaubce70882009-09-07 11:51:47 +0200894 case SRV_TPROXY_DYN:
Willy Tarreau827aee92011-03-10 16:55:02 +0100895 if (srv->bind_hdr_occ) {
Willy Tarreau294c4732011-12-16 21:35:50 +0100896 char *vptr;
897 int vlen;
898
Willy Tarreaubce70882009-09-07 11:51:47 +0200899 /* bind to the IP in a header */
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100900 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_family = AF_INET;
Willy Tarreau6471afb2011-09-23 10:54:59 +0200901 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_port = 0;
Willy Tarreau294c4732011-12-16 21:35:50 +0100902 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_addr.s_addr = 0;
903
904 if (http_get_hdr(&s->txn.req, srv->bind_hdr_name, srv->bind_hdr_len,
905 &s->txn.hdr_idx, srv->bind_hdr_occ, NULL, &vptr, &vlen)) {
906 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_addr.s_addr =
907 htonl(inetaddr_host_lim(vptr, vptr + vlen));
908 }
Willy Tarreaubce70882009-09-07 11:51:47 +0200909 }
910 break;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200911 default:
Willy Tarreau6471afb2011-09-23 10:54:59 +0200912 memset(&s->req->cons->addr.from, 0, sizeof(s->req->cons->addr.from));
Willy Tarreaub1d67742010-03-29 19:36:59 +0200913 }
914 }
915 else if (s->be->options & PR_O_BIND_SRC) {
916 switch (s->be->options & PR_O_TPXY_MASK) {
917 case PR_O_TPXY_ADDR:
Willy Tarreau6471afb2011-09-23 10:54:59 +0200918 s->req->cons->addr.from = s->be->tproxy_addr;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200919 break;
920 case PR_O_TPXY_CLI:
921 case PR_O_TPXY_CIP:
Emeric Brunec810d12010-10-22 16:36:33 +0200922 /* FIXME: what can we do if the client connects in IPv6 or socket unix? */
Willy Tarreau6471afb2011-09-23 10:54:59 +0200923 s->req->cons->addr.from = s->req->prod->addr.from;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200924 break;
Willy Tarreaubce70882009-09-07 11:51:47 +0200925 case PR_O_TPXY_DYN:
926 if (s->be->bind_hdr_occ) {
Willy Tarreau294c4732011-12-16 21:35:50 +0100927 char *vptr;
928 int vlen;
929
Willy Tarreaubce70882009-09-07 11:51:47 +0200930 /* bind to the IP in a header */
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100931 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_family = AF_INET;
Willy Tarreau6471afb2011-09-23 10:54:59 +0200932 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_port = 0;
Willy Tarreau294c4732011-12-16 21:35:50 +0100933 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_addr.s_addr = 0;
934
935 if (http_get_hdr(&s->txn.req, s->be->bind_hdr_name, s->be->bind_hdr_len,
936 &s->txn.hdr_idx, s->be->bind_hdr_occ, NULL, &vptr, &vlen)) {
937 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_addr.s_addr =
938 htonl(inetaddr_host_lim(vptr, vptr + vlen));
939 }
Willy Tarreaubce70882009-09-07 11:51:47 +0200940 }
941 break;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200942 default:
Willy Tarreau6471afb2011-09-23 10:54:59 +0200943 memset(&s->req->cons->addr.from, 0, sizeof(s->req->cons->addr.from));
Willy Tarreaub1d67742010-03-29 19:36:59 +0200944 }
945 }
946#endif
947}
948
949
Willy Tarreaubaaee002006-06-26 02:48:02 +0200950/*
951 * This function initiates a connection to the server assigned to this session
Willy Tarreau6471afb2011-09-23 10:54:59 +0200952 * (s->target, s->req->cons->addr.to). It will assign a server if none
Willy Tarreau664beb82011-03-10 11:38:29 +0100953 * is assigned yet.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200954 * It can return one of :
955 * - SN_ERR_NONE if everything's OK
956 * - SN_ERR_SRVTO if there are no more servers
957 * - SN_ERR_SRVCL if the connection was refused by the server
958 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
959 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
960 * - SN_ERR_INTERNAL for any other purely internal errors
961 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
962 */
963int connect_server(struct session *s)
964{
Willy Tarreau827aee92011-03-10 16:55:02 +0100965 struct server *srv;
Willy Tarreau9650f372009-08-16 14:02:45 +0200966 int err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200967
968 if (!(s->flags & SN_ADDR_SET)) {
969 err = assign_server_address(s);
970 if (err != SRV_STATUS_OK)
971 return SN_ERR_INTERNAL;
972 }
973
Willy Tarreaua8f55d52010-05-31 17:44:19 +0200974 /* Prepare the stream interface for a TCP connection. Later
975 * we may assign a protocol-specific connect() function.
Willy Tarreau664beb82011-03-10 11:38:29 +0100976 * NOTE: when we later support HTTP keep-alive, we'll have to
977 * decide here if we can reuse the connection by comparing the
978 * session's freshly assigned target with the stream interface's.
Willy Tarreaua8f55d52010-05-31 17:44:19 +0200979 */
980 stream_sock_prepare_interface(s->req->cons);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100981 s->req->cons->connect = tcp_connect_server;
Willy Tarreau9b061e32012-04-07 18:03:52 +0200982 s->req->cons->get_src = getsockname;
983 s->req->cons->get_dst = getpeername;
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200984 /* the target was only on the session, assign it to the SI now */
Willy Tarreau9e000c62011-03-10 14:03:36 +0100985 copy_target(&s->req->cons->target, &s->target);
Willy Tarreaud88edf22009-06-14 15:48:17 +0200986
Willy Tarreau5ab04ec2011-03-20 10:32:26 +0100987 /* process the case where the server requires the PROXY protocol to be sent */
988 s->req->cons->send_proxy_ofs = 0;
989 if (s->target.type == TARG_TYPE_SERVER && (s->target.ptr.s->state & SRV_SEND_PROXY)) {
990 s->req->cons->send_proxy_ofs = 1; /* must compute size */
Willy Tarreau9b061e32012-04-07 18:03:52 +0200991 stream_sock_get_to_addr(s->req->prod);
Willy Tarreau5ab04ec2011-03-20 10:32:26 +0100992 }
993
Willy Tarreaub1d67742010-03-29 19:36:59 +0200994 assign_tproxy_address(s);
995
William Lallemandb7ff6a32012-03-02 14:35:21 +0100996 /* flag for logging source ip/port */
997 if (s->fe->options2 & PR_O2_SRC_ADDR)
998 s->req->cons->flags |= SI_FL_SRC_ADDR;
999
Willy Tarreauac825402011-03-04 22:04:29 +01001000 err = s->req->cons->connect(s->req->cons);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001001
Willy Tarreau9650f372009-08-16 14:02:45 +02001002 if (err != SN_ERR_NONE)
1003 return err;
Willy Tarreau788e2842008-08-26 13:25:39 +02001004
Willy Tarreau827aee92011-03-10 16:55:02 +01001005 srv = target_srv(&s->target);
1006 if (srv) {
Willy Tarreau1e62de62008-11-11 20:20:02 +01001007 s->flags |= SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001008 srv->cur_sess++;
1009 if (srv->cur_sess > srv->counters.cur_sess_max)
1010 srv->counters.cur_sess_max = srv->cur_sess;
Willy Tarreau51406232008-03-10 22:04:20 +01001011 if (s->be->lbprm.server_take_conn)
Willy Tarreau827aee92011-03-10 16:55:02 +01001012 s->be->lbprm.server_take_conn(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001013 }
1014
Willy Tarreaubaaee002006-06-26 02:48:02 +02001015 return SN_ERR_NONE; /* connection is OK */
1016}
1017
1018
Willy Tarreaubaaee002006-06-26 02:48:02 +02001019/* This function performs the "redispatch" part of a connection attempt. It
1020 * will assign a server if required, queue the connection if required, and
1021 * handle errors that might arise at this level. It can change the server
1022 * state. It will return 1 if it encounters an error, switches the server
1023 * state, or has to queue a connection. Otherwise, it will return 0 indicating
1024 * that the connection is ready to use.
1025 */
1026
1027int srv_redispatch_connect(struct session *t)
1028{
Willy Tarreau827aee92011-03-10 16:55:02 +01001029 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001030 int conn_err;
1031
1032 /* We know that we don't have any connection pending, so we will
1033 * try to get a new one, and wait in this state if it's queued
1034 */
Willy Tarreau7c669d72008-06-20 15:04:11 +02001035 redispatch:
Willy Tarreaubaaee002006-06-26 02:48:02 +02001036 conn_err = assign_server_and_queue(t);
Willy Tarreau827aee92011-03-10 16:55:02 +01001037 srv = target_srv(&t->target);
1038
Willy Tarreaubaaee002006-06-26 02:48:02 +02001039 switch (conn_err) {
1040 case SRV_STATUS_OK:
1041 break;
1042
Willy Tarreau7c669d72008-06-20 15:04:11 +02001043 case SRV_STATUS_FULL:
1044 /* The server has reached its maxqueue limit. Either PR_O_REDISP is set
1045 * and we can redispatch to another server, or it is not and we return
1046 * 503. This only makes sense in DIRECT mode however, because normal LB
1047 * algorithms would never select such a server, and hash algorithms
Willy Tarreau827aee92011-03-10 16:55:02 +01001048 * would bring us on the same server again. Note that t->target is set
1049 * in this case.
Willy Tarreau7c669d72008-06-20 15:04:11 +02001050 */
Willy Tarreau4de91492010-01-22 19:10:05 +01001051 if (((t->flags & (SN_DIRECT|SN_FORCE_PRST)) == SN_DIRECT) &&
1052 (t->be->options & PR_O_REDISP)) {
Willy Tarreau7c669d72008-06-20 15:04:11 +02001053 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Willy Tarreau7c669d72008-06-20 15:04:11 +02001054 goto redispatch;
1055 }
1056
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001057 if (!t->req->cons->err_type) {
1058 t->req->cons->err_type = SI_ET_QUEUE_ERR;
Willy Tarreau827aee92011-03-10 16:55:02 +01001059 t->req->cons->err_loc = srv;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001060 }
Willy Tarreau7c669d72008-06-20 15:04:11 +02001061
Willy Tarreau827aee92011-03-10 16:55:02 +01001062 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001063 t->be->be_counters.failed_conns++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02001064 return 1;
1065
Willy Tarreaubaaee002006-06-26 02:48:02 +02001066 case SRV_STATUS_NOSRV:
Willy Tarreau827aee92011-03-10 16:55:02 +01001067 /* note: it is guaranteed that srv == NULL here */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001068 if (!t->req->cons->err_type) {
1069 t->req->cons->err_type = SI_ET_CONN_ERR;
1070 t->req->cons->err_loc = NULL;
1071 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +01001072
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001073 t->be->be_counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001074 return 1;
1075
1076 case SRV_STATUS_QUEUED:
Willy Tarreau35374672008-09-03 18:11:02 +02001077 t->req->cons->exp = tick_add_ifset(now_ms, t->be->timeout.queue);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001078 t->req->cons->state = SI_ST_QUE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001079 /* do nothing else and do not wake any other session up */
1080 return 1;
1081
Willy Tarreaubaaee002006-06-26 02:48:02 +02001082 case SRV_STATUS_INTERNAL:
1083 default:
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001084 if (!t->req->cons->err_type) {
1085 t->req->cons->err_type = SI_ET_CONN_OTHER;
Willy Tarreau827aee92011-03-10 16:55:02 +01001086 t->req->cons->err_loc = srv;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001087 }
1088
Willy Tarreau827aee92011-03-10 16:55:02 +01001089 if (srv)
1090 srv_inc_sess_ctr(srv);
1091 if (srv)
1092 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001093 t->be->be_counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001094
1095 /* release other sessions waiting for this server */
Willy Tarreau827aee92011-03-10 16:55:02 +01001096 if (may_dequeue_tasks(srv, t->be))
1097 process_srv_queue(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001098 return 1;
1099 }
1100 /* if we get here, it's because we got SRV_STATUS_OK, which also
1101 * means that the connection has not been queued.
1102 */
1103 return 0;
1104}
1105
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001106/* Apply RDP cookie persistence to the current session. For this, the function
1107 * tries to extract an RDP cookie from the request buffer, and look for the
1108 * matching server in the list. If the server is found, it is assigned to the
1109 * session. This always returns 1, and the analyser removes itself from the
1110 * list. Nothing is performed if a server was already assigned.
1111 */
1112int tcp_persist_rdp_cookie(struct session *s, struct buffer *req, int an_bit)
1113{
1114 struct proxy *px = s->be;
1115 int ret;
1116 struct acl_expr expr;
Willy Tarreau37406352012-04-23 16:16:37 +02001117 struct sample smp;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001118 struct server *srv = px->srv;
1119 struct sockaddr_in addr;
1120 char *p;
Willy Tarreau34db1082012-04-19 17:16:54 +02001121 struct arg args[2];
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001122
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001123 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 +02001124 now_ms, __FUNCTION__,
1125 s,
1126 req,
1127 req->rex, req->wex,
1128 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001129 req->i,
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001130 req->analysers);
1131
1132 if (s->flags & SN_ASSIGNED)
1133 goto no_cookie;
1134
1135 memset(&expr, 0, sizeof(expr));
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
1143 expr.args = args;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001144
Willy Tarreau37406352012-04-23 16:16:37 +02001145 ret = acl_fetch_rdp_cookie(px, s, NULL, ACL_DIR_REQ, &expr, &smp);
Willy Tarreauf853c462012-04-23 18:53:56 +02001146 if (ret == 0 || (smp.flags & SMP_F_MAY_CHANGE) || smp.data.str.len == 0)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001147 goto no_cookie;
1148
1149 memset(&addr, 0, sizeof(addr));
1150 addr.sin_family = AF_INET;
1151
Willy Tarreau664092c2011-12-16 19:11:42 +01001152 /* Considering an rdp cookie detected using acl, str ended with <cr><lf> and should return */
Willy Tarreauf853c462012-04-23 18:53:56 +02001153 addr.sin_addr.s_addr = strtoul(smp.data.str.str, &p, 10);
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001154 if (*p != '.')
1155 goto no_cookie;
1156 p++;
1157 addr.sin_port = (unsigned short)strtoul(p, &p, 10);
1158 if (*p != '.')
1159 goto no_cookie;
1160
Willy Tarreau9e000c62011-03-10 14:03:36 +01001161 clear_target(&s->target);
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001162 while (srv) {
1163 if (memcmp(&addr, &(srv->addr), sizeof(addr)) == 0) {
1164 if ((srv->state & SRV_RUNNING) || (px->options & PR_O_PERSIST)) {
1165 /* we found the server and it is usable */
1166 s->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01001167 set_target_server(&s->target, srv);
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001168 break;
1169 }
1170 }
1171 srv = srv->next;
1172 }
1173
1174no_cookie:
1175 req->analysers &= ~an_bit;
1176 req->analyse_exp = TICK_ETERNITY;
1177 return 1;
1178}
1179
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001180int be_downtime(struct proxy *px) {
Willy Tarreaub625a082007-11-26 01:15:43 +01001181 if (px->lbprm.tot_weight && px->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001182 return px->down_time;
1183
1184 return now.tv_sec - px->last_change + px->down_time;
1185}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001186
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001187/*
1188 * This function returns a string containing the balancing
1189 * mode of the proxy in a format suitable for stats.
1190 */
1191
1192const char *backend_lb_algo_str(int algo) {
1193
1194 if (algo == BE_LB_ALGO_RR)
1195 return "roundrobin";
1196 else if (algo == BE_LB_ALGO_SRR)
1197 return "static-rr";
Willy Tarreauf09c6602012-02-13 17:12:08 +01001198 else if (algo == BE_LB_ALGO_FAS)
1199 return "first";
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001200 else if (algo == BE_LB_ALGO_LC)
1201 return "leastconn";
1202 else if (algo == BE_LB_ALGO_SH)
1203 return "source";
1204 else if (algo == BE_LB_ALGO_UH)
1205 return "uri";
1206 else if (algo == BE_LB_ALGO_PH)
1207 return "url_param";
1208 else if (algo == BE_LB_ALGO_HH)
1209 return "hdr";
1210 else if (algo == BE_LB_ALGO_RCH)
1211 return "rdp-cookie";
1212 else
1213 return NULL;
1214}
1215
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001216/* This function parses a "balance" statement in a backend section describing
1217 * <curproxy>. It returns -1 if there is any error, otherwise zero. If it
1218 * returns -1, it may write an error message into ther <err> buffer, for at
1219 * most <errlen> bytes, trailing zero included. The trailing '\n' will not be
1220 * written. The function must be called with <args> pointing to the first word
1221 * after "balance".
1222 */
1223int backend_parse_balance(const char **args, char *err, int errlen, struct proxy *curproxy)
1224{
1225 if (!*(args[0])) {
1226 /* if no option is set, use round-robin by default */
Willy Tarreau31682232007-11-29 15:38:04 +01001227 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1228 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001229 return 0;
1230 }
1231
1232 if (!strcmp(args[0], "roundrobin")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001233 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1234 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001235 }
Willy Tarreau9757a382009-10-03 12:56:50 +02001236 else if (!strcmp(args[0], "static-rr")) {
1237 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1238 curproxy->lbprm.algo |= BE_LB_ALGO_SRR;
1239 }
Willy Tarreauf09c6602012-02-13 17:12:08 +01001240 else if (!strcmp(args[0], "first")) {
1241 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1242 curproxy->lbprm.algo |= BE_LB_ALGO_FAS;
1243 }
Willy Tarreau51406232008-03-10 22:04:20 +01001244 else if (!strcmp(args[0], "leastconn")) {
1245 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1246 curproxy->lbprm.algo |= BE_LB_ALGO_LC;
1247 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001248 else if (!strcmp(args[0], "source")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001249 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1250 curproxy->lbprm.algo |= BE_LB_ALGO_SH;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001251 }
1252 else if (!strcmp(args[0], "uri")) {
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001253 int arg = 1;
1254
Willy Tarreau31682232007-11-29 15:38:04 +01001255 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1256 curproxy->lbprm.algo |= BE_LB_ALGO_UH;
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001257
1258 while (*args[arg]) {
1259 if (!strcmp(args[arg], "len")) {
1260 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
1261 snprintf(err, errlen, "'balance uri len' expects a positive integer (got '%s').", args[arg+1]);
1262 return -1;
1263 }
1264 curproxy->uri_len_limit = atoi(args[arg+1]);
1265 arg += 2;
1266 }
1267 else if (!strcmp(args[arg], "depth")) {
1268 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
1269 snprintf(err, errlen, "'balance uri depth' expects a positive integer (got '%s').", args[arg+1]);
1270 return -1;
1271 }
1272 /* hint: we store the position of the ending '/' (depth+1) so
1273 * that we avoid a comparison while computing the hash.
1274 */
1275 curproxy->uri_dirs_depth1 = atoi(args[arg+1]) + 1;
1276 arg += 2;
1277 }
1278 else {
1279 snprintf(err, errlen, "'balance uri' only accepts parameters 'len' and 'depth' (got '%s').", args[arg]);
1280 return -1;
1281 }
1282 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001283 }
Willy Tarreau01732802007-11-01 22:48:15 +01001284 else if (!strcmp(args[0], "url_param")) {
1285 if (!*args[1]) {
1286 snprintf(err, errlen, "'balance url_param' requires an URL parameter name.");
1287 return -1;
1288 }
Willy Tarreau31682232007-11-29 15:38:04 +01001289 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1290 curproxy->lbprm.algo |= BE_LB_ALGO_PH;
Willy Tarreaua534fea2008-08-03 12:19:50 +02001291
1292 free(curproxy->url_param_name);
Willy Tarreau01732802007-11-01 22:48:15 +01001293 curproxy->url_param_name = strdup(args[1]);
Willy Tarreaua534fea2008-08-03 12:19:50 +02001294 curproxy->url_param_len = strlen(args[1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001295 if (*args[2]) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001296 if (strcmp(args[2], "check_post")) {
1297 snprintf(err, errlen, "'balance url_param' only accepts check_post modifier.");
1298 return -1;
1299 }
1300 if (*args[3]) {
1301 /* TODO: maybe issue a warning if there is no value, no digits or too long */
1302 curproxy->url_param_post_limit = str2ui(args[3]);
1303 }
1304 /* if no limit, or faul value in args[3], then default to a moderate wordlen */
1305 if (!curproxy->url_param_post_limit)
1306 curproxy->url_param_post_limit = 48;
1307 else if ( curproxy->url_param_post_limit < 3 )
1308 curproxy->url_param_post_limit = 3; /* minimum example: S=3 or \r\nS=6& */
1309 }
Benoitaffb4812009-03-25 13:02:10 +01001310 }
1311 else if (!strncmp(args[0], "hdr(", 4)) {
1312 const char *beg, *end;
1313
1314 beg = args[0] + 4;
1315 end = strchr(beg, ')');
1316
1317 if (!end || end == beg) {
1318 snprintf(err, errlen, "'balance hdr(name)' requires an http header field name.");
1319 return -1;
1320 }
1321
1322 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1323 curproxy->lbprm.algo |= BE_LB_ALGO_HH;
1324
1325 free(curproxy->hh_name);
1326 curproxy->hh_len = end - beg;
1327 curproxy->hh_name = my_strndup(beg, end - beg);
1328 curproxy->hh_match_domain = 0;
1329
1330 if (*args[1]) {
1331 if (strcmp(args[1], "use_domain_only")) {
1332 snprintf(err, errlen, "'balance hdr(name)' only accepts 'use_domain_only' modifier.");
1333 return -1;
1334 }
1335 curproxy->hh_match_domain = 1;
1336 }
1337
Emeric Brun736aa232009-06-30 17:56:00 +02001338 }
1339 else if (!strncmp(args[0], "rdp-cookie", 10)) {
1340 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1341 curproxy->lbprm.algo |= BE_LB_ALGO_RCH;
1342
1343 if ( *(args[0] + 10 ) == '(' ) { /* cookie name */
1344 const char *beg, *end;
1345
1346 beg = args[0] + 11;
1347 end = strchr(beg, ')');
1348
1349 if (!end || end == beg) {
1350 snprintf(err, errlen, "'balance rdp-cookie(name)' requires an rdp cookie name.");
1351 return -1;
1352 }
1353
1354 free(curproxy->hh_name);
1355 curproxy->hh_name = my_strndup(beg, end - beg);
1356 curproxy->hh_len = end - beg;
1357 }
1358 else if ( *(args[0] + 10 ) == '\0' ) { /* default cookie name 'mstshash' */
1359 free(curproxy->hh_name);
1360 curproxy->hh_name = strdup("mstshash");
1361 curproxy->hh_len = strlen(curproxy->hh_name);
1362 }
1363 else { /* syntax */
1364 snprintf(err, errlen, "'balance rdp-cookie(name)' requires an rdp cookie name.");
1365 return -1;
1366 }
Willy Tarreau01732802007-11-01 22:48:15 +01001367 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001368 else {
Willy Tarreau9757a382009-10-03 12:56:50 +02001369 snprintf(err, errlen, "'balance' only supports 'roundrobin', 'static-rr', 'leastconn', 'source', 'uri', 'url_param', 'hdr(name)' and 'rdp-cookie(name)' options.");
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001370 return -1;
1371 }
1372 return 0;
1373}
1374
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001375
1376/************************************************************************/
1377/* All supported keywords must be declared here. */
1378/************************************************************************/
1379
Willy Tarreau34db1082012-04-19 17:16:54 +02001380/* set temp integer to the number of enabled servers on the proxy.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001381 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001382 * undefined behaviour.
1383 */
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001384static int
1385acl_fetch_nbsrv(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau37406352012-04-23 16:16:37 +02001386 struct acl_expr *expr, struct sample *smp)
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001387{
Willy Tarreau37406352012-04-23 16:16:37 +02001388 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001389 smp->type = SMP_T_UINT;
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001390 px = expr->args->data.prx;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001391
1392 if (px->srv_act)
Willy Tarreauf853c462012-04-23 18:53:56 +02001393 smp->data.uint = px->srv_act;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001394 else if (px->lbprm.fbck)
Willy Tarreauf853c462012-04-23 18:53:56 +02001395 smp->data.uint = 1;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001396 else
Willy Tarreauf853c462012-04-23 18:53:56 +02001397 smp->data.uint = px->srv_bck;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001398
1399 return 1;
1400}
1401
Willy Tarreau37406352012-04-23 16:16:37 +02001402/* report in smp->flags a success or failure depending on the designated
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001403 * server's state. There is no match function involved since there's no pattern.
Willy Tarreau34db1082012-04-19 17:16:54 +02001404 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1405 * undefined behaviour.
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001406 */
1407static int
1408acl_fetch_srv_is_up(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau37406352012-04-23 16:16:37 +02001409 struct acl_expr *expr, struct sample *smp)
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001410{
Willy Tarreau34db1082012-04-19 17:16:54 +02001411 struct server *srv = expr->args->data.srv;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001412
Willy Tarreau37406352012-04-23 16:16:37 +02001413 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001414 smp->type = SMP_T_BOOL;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001415 if (!(srv->state & SRV_MAINTAIN) &&
1416 (!(srv->state & SRV_CHECKED) || (srv->state & SRV_RUNNING)))
Willy Tarreau197e10a2012-04-23 19:18:42 +02001417 smp->data.uint = 1;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001418 else
Willy Tarreau197e10a2012-04-23 19:18:42 +02001419 smp->data.uint = 0;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001420 return 1;
1421}
1422
Willy Tarreau34db1082012-04-19 17:16:54 +02001423/* set temp integer to the number of enabled servers on the proxy.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001424 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001425 * undefined behaviour.
1426 */
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001427static int
1428acl_fetch_connslots(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau37406352012-04-23 16:16:37 +02001429 struct acl_expr *expr, struct sample *smp)
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001430{
1431 struct server *iterator;
Willy Tarreaud28c3532012-04-19 19:28:33 +02001432
Willy Tarreau37406352012-04-23 16:16:37 +02001433 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001434 smp->type = SMP_T_UINT;
1435 smp->data.uint = 0;
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001436
1437 for (iterator = expr->args->data.prx->srv; iterator; iterator = iterator->next) {
1438 if ((iterator->state & SRV_RUNNING) == 0)
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001439 continue;
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001440
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001441 if (iterator->maxconn == 0 || iterator->maxqueue == 0) {
Willy Tarreaua5e37562011-12-16 17:06:15 +01001442 /* configuration is stupid */
Willy Tarreauf853c462012-04-23 18:53:56 +02001443 smp->data.uint = -1; /* FIXME: stupid value! */
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001444 return 1;
1445 }
1446
Willy Tarreauf853c462012-04-23 18:53:56 +02001447 smp->data.uint += (iterator->maxconn - iterator->cur_sess)
Willy Tarreau422aa072012-04-20 20:49:27 +02001448 + (iterator->maxqueue - iterator->nbpend);
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001449 }
1450
1451 return 1;
1452}
1453
Willy Tarreaua5e37562011-12-16 17:06:15 +01001454/* set temp integer to the id of the backend */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001455static int
1456acl_fetch_be_id(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau37406352012-04-23 16:16:37 +02001457 struct acl_expr *expr, struct sample *smp)
1458{
1459 smp->flags = SMP_F_READ_ONLY;
Willy Tarreauf853c462012-04-23 18:53:56 +02001460 smp->flags = SMP_F_VOL_TXN;
1461 smp->type = SMP_T_UINT;
1462 smp->data.uint = l4->be->uuid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001463 return 1;
1464}
1465
Willy Tarreaua5e37562011-12-16 17:06:15 +01001466/* set temp integer to the id of the server */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001467static int
1468acl_fetch_srv_id(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau37406352012-04-23 16:16:37 +02001469 struct acl_expr *expr, struct sample *smp)
1470{
Willy Tarreau827aee92011-03-10 16:55:02 +01001471 if (!target_srv(&l4->target))
Willy Tarreau17af4192011-02-23 14:27:06 +01001472 return 0;
1473
Willy Tarreau37406352012-04-23 16:16:37 +02001474 smp->flags = SMP_F_READ_ONLY;
Willy Tarreauf853c462012-04-23 18:53:56 +02001475 smp->type = SMP_T_UINT;
1476 smp->data.uint = target_srv(&l4->target)->puid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001477
1478 return 1;
1479}
1480
Willy Tarreau34db1082012-04-19 17:16:54 +02001481/* set temp integer to the number of connections per second reaching the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001482 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001483 * undefined behaviour.
1484 */
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001485static int
1486acl_fetch_be_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau37406352012-04-23 16:16:37 +02001487 struct acl_expr *expr, struct sample *smp)
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001488{
Willy Tarreau37406352012-04-23 16:16:37 +02001489 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001490 smp->type = SMP_T_UINT;
1491 smp->data.uint = read_freq_ctr(&expr->args->data.prx->be_sess_per_sec);
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001492 return 1;
1493}
1494
Willy Tarreau34db1082012-04-19 17:16:54 +02001495/* set temp integer to the number of concurrent connections on the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001496 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001497 * undefined behaviour.
1498 */
Willy Tarreaua36af912009-10-10 12:02:45 +02001499static int
1500acl_fetch_be_conn(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau37406352012-04-23 16:16:37 +02001501 struct acl_expr *expr, struct sample *smp)
Willy Tarreaua36af912009-10-10 12:02:45 +02001502{
Willy Tarreau37406352012-04-23 16:16:37 +02001503 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001504 smp->type = SMP_T_UINT;
1505 smp->data.uint = expr->args->data.prx->beconn;
Willy Tarreaua36af912009-10-10 12:02:45 +02001506 return 1;
1507}
1508
Willy Tarreau34db1082012-04-19 17:16:54 +02001509/* set temp integer to the total number of queued connections on the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001510 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001511 * undefined behaviour.
1512 */
Willy Tarreaua36af912009-10-10 12:02:45 +02001513static int
1514acl_fetch_queue_size(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau37406352012-04-23 16:16:37 +02001515 struct acl_expr *expr, struct sample *smp)
Willy Tarreaua36af912009-10-10 12:02:45 +02001516{
Willy Tarreau37406352012-04-23 16:16:37 +02001517 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001518 smp->type = SMP_T_UINT;
1519 smp->data.uint = expr->args->data.prx->totpend;
Willy Tarreaua36af912009-10-10 12:02:45 +02001520 return 1;
1521}
1522
Willy Tarreaua5e37562011-12-16 17:06:15 +01001523/* set temp integer to the total number of queued connections on the backend divided
Willy Tarreaua36af912009-10-10 12:02:45 +02001524 * by the number of running servers and rounded up. If there is no running
1525 * server, we return twice the total, just as if we had half a running server.
1526 * This is more or less correct anyway, since we expect the last server to come
1527 * back soon.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001528 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001529 * undefined behaviour.
Willy Tarreaua36af912009-10-10 12:02:45 +02001530 */
1531static int
1532acl_fetch_avg_queue_size(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau37406352012-04-23 16:16:37 +02001533 struct acl_expr *expr, struct sample *smp)
Willy Tarreaua36af912009-10-10 12:02:45 +02001534{
1535 int nbsrv;
1536
Willy Tarreau37406352012-04-23 16:16:37 +02001537 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001538 smp->type = SMP_T_UINT;
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001539 px = expr->args->data.prx;
Willy Tarreaua36af912009-10-10 12:02:45 +02001540
1541 if (px->srv_act)
1542 nbsrv = px->srv_act;
1543 else if (px->lbprm.fbck)
1544 nbsrv = 1;
1545 else
1546 nbsrv = px->srv_bck;
1547
1548 if (nbsrv > 0)
Willy Tarreauf853c462012-04-23 18:53:56 +02001549 smp->data.uint = (px->totpend + nbsrv - 1) / nbsrv;
Willy Tarreaua36af912009-10-10 12:02:45 +02001550 else
Willy Tarreauf853c462012-04-23 18:53:56 +02001551 smp->data.uint = px->totpend * 2;
Willy Tarreaua36af912009-10-10 12:02:45 +02001552
1553 return 1;
1554}
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001555
Willy Tarreau34db1082012-04-19 17:16:54 +02001556/* set temp integer to the number of concurrent connections on the server in the backend.
1557 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1558 * undefined behaviour.
1559 */
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001560static int
1561acl_fetch_srv_conn(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau37406352012-04-23 16:16:37 +02001562 struct acl_expr *expr, struct sample *smp)
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001563{
Willy Tarreauf853c462012-04-23 18:53:56 +02001564 smp->flags = SMP_F_VOL_TEST;
1565 smp->type = SMP_T_UINT;
1566 smp->data.uint = expr->args->data.srv->cur_sess;
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001567 return 1;
1568}
1569
Willy Tarreau61612d42012-04-19 18:42:05 +02001570/* Note: must not be declared <const> as its list will be overwritten.
1571 * Please take care of keeping this list alphabetically sorted.
1572 */
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001573static struct acl_kw_list acl_kws = {{ },{
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001574 { "avg_queue", acl_parse_int, acl_fetch_avg_queue_size, acl_match_int, ACL_USE_NOTHING, ARG1(1,BE) },
1575 { "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 +02001576 { "be_id", acl_parse_int, acl_fetch_be_id, acl_match_int, ACL_USE_NOTHING, 0 },
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001577 { "be_sess_rate", acl_parse_int, acl_fetch_be_sess_rate, acl_match_int, ACL_USE_NOTHING, ARG1(1,BE) },
1578 { "connslots", acl_parse_int, acl_fetch_connslots, acl_match_int, ACL_USE_NOTHING, ARG1(1,BE) },
1579 { "nbsrv", acl_parse_int, acl_fetch_nbsrv, acl_match_int, ACL_USE_NOTHING, ARG1(1,BE) },
1580 { "queue", acl_parse_int, acl_fetch_queue_size, acl_match_int, ACL_USE_NOTHING, ARG1(1,BE) },
Willy Tarreau61612d42012-04-19 18:42:05 +02001581 { "srv_conn", acl_parse_int, acl_fetch_srv_conn, acl_match_int, ACL_USE_NOTHING, ARG1(1,SRV) },
1582 { "srv_id", acl_parse_int, acl_fetch_srv_id, acl_match_int, ACL_USE_RTR_INTERNAL, 0 },
1583 { "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 +01001584 { NULL, NULL, NULL, NULL },
1585}};
1586
1587
1588__attribute__((constructor))
1589static void __backend_init(void)
1590{
1591 acl_register_keywords(&acl_kws);
1592}
1593
Willy Tarreaubaaee002006-06-26 02:48:02 +02001594/*
1595 * Local variables:
1596 * c-indent-level: 8
1597 * c-basic-offset: 8
1598 * End:
1599 */