blob: a9567070ff498f5b834a66dfb20ce75fcd93d36d [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;
Willy Tarreau37406352012-04-23 16:16:37 +0200405 struct sample smp;
Willy Tarreau34db1082012-04-19 17:16:54 +0200406 struct arg args[2];
Emeric Brun736aa232009-06-30 17:56:00 +0200407
408 /* tot_weight appears to mean srv_count */
409 if (px->lbprm.tot_weight == 0)
410 return NULL;
411
Willy Tarreau37406352012-04-23 16:16:37 +0200412 memset(&smp, 0, sizeof(smp));
Emeric Brun736aa232009-06-30 17:56:00 +0200413
Willy Tarreau34db1082012-04-19 17:16:54 +0200414 args[0].type = ARGT_STR;
415 args[0].data.str.str = px->hh_name;
416 args[0].data.str.len = px->hh_len;
417 args[1].type = ARGT_STOP;
418
Willy Tarreau32389b72012-04-23 23:13:20 +0200419 ret = smp_fetch_rdp_cookie(px, s, NULL, ACL_DIR_REQ, args, &smp);
Willy Tarreauf853c462012-04-23 18:53:56 +0200420 len = smp.data.str.len;
Willy Tarreau664092c2011-12-16 19:11:42 +0100421
Willy Tarreau37406352012-04-23 16:16:37 +0200422 if (ret == 0 || (smp.flags & SMP_F_MAY_CHANGE) || len == 0)
Emeric Brun736aa232009-06-30 17:56:00 +0200423 return NULL;
424
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200425 /* note: we won't hash if there's only one server left */
426 if (px->lbprm.tot_used == 1)
427 goto hash_done;
428
Emeric Brun736aa232009-06-30 17:56:00 +0200429 /* Found a the hh_name in the headers.
430 * we will compute the hash based on this value ctx.val.
431 */
Willy Tarreauf853c462012-04-23 18:53:56 +0200432 p = smp.data.str.str;
Emeric Brun736aa232009-06-30 17:56:00 +0200433 while (len) {
434 hash = *p + (hash << 6) + (hash << 16) - hash;
435 len--;
436 p++;
437 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100438 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
439 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200440 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200441 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
442 return chash_get_server_hash(px, hash);
443 else
444 return map_get_server_hash(px, hash);
Emeric Brun736aa232009-06-30 17:56:00 +0200445}
Benoitaffb4812009-03-25 13:02:10 +0100446
447/*
Willy Tarreau7c669d72008-06-20 15:04:11 +0200448 * This function applies the load-balancing algorithm to the session, as
449 * defined by the backend it is assigned to. The session is then marked as
450 * 'assigned'.
451 *
452 * This function MAY NOT be called with SN_ASSIGNED already set. If the session
453 * had a server previously assigned, it is rebalanced, trying to avoid the same
Willy Tarreau827aee92011-03-10 16:55:02 +0100454 * server, which should still be present in target_srv(&s->target) before the call.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200455 * The function tries to keep the original connection slot if it reconnects to
456 * the same server, otherwise it releases it and tries to offer it.
457 *
458 * It is illegal to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200459 *
460 * It may return :
Willy Tarreau664beb82011-03-10 11:38:29 +0100461 * SRV_STATUS_OK if everything is OK. ->srv and ->target are assigned.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200462 * SRV_STATUS_NOSRV if no server is available. Session is not ASSIGNED
463 * SRV_STATUS_FULL if all servers are saturated. Session is not ASSIGNED
Willy Tarreaubaaee002006-06-26 02:48:02 +0200464 * SRV_STATUS_INTERNAL for other unrecoverable errors.
465 *
Willy Tarreau7c669d72008-06-20 15:04:11 +0200466 * Upon successful return, the session flag SN_ASSIGNED is set to indicate that
Willy Tarreau827aee92011-03-10 16:55:02 +0100467 * it does not need to be called anymore. This means that target_srv(&s->target)
468 * can be trusted in balance and direct modes.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200469 *
470 */
471
472int assign_server(struct session *s)
473{
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100474
Willy Tarreau7c669d72008-06-20 15:04:11 +0200475 struct server *conn_slot;
Willy Tarreau827aee92011-03-10 16:55:02 +0100476 struct server *srv, *prev_srv;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200477 int err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100478
Simon Horman8effd3d2011-08-13 08:03:52 +0900479 DPRINTF(stderr,"assign_server : s=%p\n",s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200480
Willy Tarreau7c669d72008-06-20 15:04:11 +0200481 err = SRV_STATUS_INTERNAL;
482 if (unlikely(s->pend_pos || s->flags & SN_ASSIGNED))
483 goto out_err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100484
Willy Tarreau827aee92011-03-10 16:55:02 +0100485 prev_srv = target_srv(&s->target);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200486 conn_slot = s->srv_conn;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200487
Willy Tarreau7c669d72008-06-20 15:04:11 +0200488 /* We have to release any connection slot before applying any LB algo,
489 * otherwise we may erroneously end up with no available slot.
490 */
491 if (conn_slot)
492 sess_change_server(s, NULL);
493
Willy Tarreau827aee92011-03-10 16:55:02 +0100494 /* We will now try to find the good server and store it into <target_srv(&s->target)>.
495 * Note that <target_srv(&s->target)> may be NULL in case of dispatch or proxy mode,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200496 * as well as if no server is available (check error code).
497 */
Willy Tarreau1a20a5d2007-11-01 21:08:19 +0100498
Willy Tarreau827aee92011-03-10 16:55:02 +0100499 srv = NULL;
Willy Tarreau9e000c62011-03-10 14:03:36 +0100500 clear_target(&s->target);
Willy Tarreau664beb82011-03-10 11:38:29 +0100501
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200502 if (s->be->lbprm.algo & BE_LB_KIND) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200503 /* we must check if we have at least one server available */
504 if (!s->be->lbprm.tot_weight) {
505 err = SRV_STATUS_NOSRV;
506 goto out;
507 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200508
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200509 /* First check whether we need to fetch some data or simply call
510 * the LB lookup function. Only the hashing functions will need
511 * some input data in fact, and will support multiple algorithms.
512 */
513 switch (s->be->lbprm.algo & BE_LB_LKUP) {
514 case BE_LB_LKUP_RRTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100515 srv = fwrr_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200516 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200517
Willy Tarreauf09c6602012-02-13 17:12:08 +0100518 case BE_LB_LKUP_FSTREE:
519 srv = fas_get_next_server(s->be, prev_srv);
520 break;
521
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200522 case BE_LB_LKUP_LCTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100523 srv = fwlc_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200524 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200525
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200526 case BE_LB_LKUP_CHTREE:
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200527 case BE_LB_LKUP_MAP:
Willy Tarreau9757a382009-10-03 12:56:50 +0200528 if ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200529 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100530 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200531 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100532 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau9757a382009-10-03 12:56:50 +0200533 break;
534 }
535 else if ((s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_HI) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200536 /* unknown balancing algorithm */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200537 err = SRV_STATUS_INTERNAL;
538 goto out;
539 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200540
541 switch (s->be->lbprm.algo & BE_LB_PARM) {
542 case BE_LB_HASH_SRC:
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200543 if (s->req->prod->addr.from.ss_family == AF_INET) {
544 srv = get_server_sh(s->be,
545 (void *)&((struct sockaddr_in *)&s->req->prod->addr.from)->sin_addr,
546 4);
547 }
548 else if (s->req->prod->addr.from.ss_family == AF_INET6) {
549 srv = get_server_sh(s->be,
550 (void *)&((struct sockaddr_in6 *)&s->req->prod->addr.from)->sin6_addr,
551 16);
552 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200553 else {
554 /* unknown IP family */
555 err = SRV_STATUS_INTERNAL;
556 goto out;
557 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200558 break;
559
560 case BE_LB_HASH_URI:
561 /* URI hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100562 if (s->txn.req.msg_state < HTTP_MSG_BODY)
563 break;
Willy Tarreau827aee92011-03-10 16:55:02 +0100564 srv = get_server_uh(s->be,
Willy Tarreau3a215be2012-03-09 21:39:51 +0100565 s->req->p + s->txn.req.sol + s->txn.req.sl.rq.u,
Willy Tarreau827aee92011-03-10 16:55:02 +0100566 s->txn.req.sl.rq.u_l);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200567 break;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200568
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200569 case BE_LB_HASH_PRM:
570 /* URL Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100571 if (s->txn.req.msg_state < HTTP_MSG_BODY)
572 break;
Willy Tarreau61a21a32011-03-01 20:35:49 +0100573
Willy Tarreau827aee92011-03-10 16:55:02 +0100574 srv = get_server_ph(s->be,
Willy Tarreau3a215be2012-03-09 21:39:51 +0100575 s->req->p + s->txn.req.sol + s->txn.req.sl.rq.u,
Willy Tarreau827aee92011-03-10 16:55:02 +0100576 s->txn.req.sl.rq.u_l);
Willy Tarreau61a21a32011-03-01 20:35:49 +0100577
Willy Tarreau827aee92011-03-10 16:55:02 +0100578 if (!srv && s->txn.meth == HTTP_METH_POST)
579 srv = get_server_ph_post(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200580 break;
Benoitaffb4812009-03-25 13:02:10 +0100581
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200582 case BE_LB_HASH_HDR:
583 /* Header Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100584 if (s->txn.req.msg_state < HTTP_MSG_BODY)
585 break;
Willy Tarreau827aee92011-03-10 16:55:02 +0100586 srv = get_server_hh(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200587 break;
588
589 case BE_LB_HASH_RDP:
590 /* RDP Cookie hashing */
Willy Tarreau827aee92011-03-10 16:55:02 +0100591 srv = get_server_rch(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200592 break;
593
594 default:
595 /* unknown balancing algorithm */
596 err = SRV_STATUS_INTERNAL;
597 goto out;
Benoitaffb4812009-03-25 13:02:10 +0100598 }
Emeric Brun736aa232009-06-30 17:56:00 +0200599
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200600 /* If the hashing parameter was not found, let's fall
601 * back to round robin on the map.
602 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100603 if (!srv) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200604 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100605 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200606 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100607 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200608 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200609
610 /* end of map-based LB */
Emeric Brun736aa232009-06-30 17:56:00 +0200611 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200612
Willy Tarreau7c669d72008-06-20 15:04:11 +0200613 default:
614 /* unknown balancing algorithm */
615 err = SRV_STATUS_INTERNAL;
616 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200617 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200618
Willy Tarreau827aee92011-03-10 16:55:02 +0100619 if (!srv) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200620 err = SRV_STATUS_FULL;
621 goto out;
622 }
Willy Tarreau827aee92011-03-10 16:55:02 +0100623 else if (srv != prev_srv) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100624 s->be->be_counters.cum_lbconn++;
Willy Tarreau827aee92011-03-10 16:55:02 +0100625 srv->counters.cum_lbconn++;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100626 }
Willy Tarreau827aee92011-03-10 16:55:02 +0100627 set_target_server(&s->target, srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200628 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200629 else if (s->be->options & (PR_O_DISPATCH | PR_O_TRANSP)) {
Willy Tarreau9e000c62011-03-10 14:03:36 +0100630 set_target_proxy(&s->target, s->be);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200631 }
David du Colombier6f5ccb12011-03-10 22:26:24 +0100632 else if ((s->be->options & PR_O_HTTP_PROXY) &&
Willy Tarreau6471afb2011-09-23 10:54:59 +0200633 is_addr(&s->req->cons->addr.to)) {
Willy Tarreau664beb82011-03-10 11:38:29 +0100634 /* in proxy mode, we need a valid destination address */
Willy Tarreau9e000c62011-03-10 14:03:36 +0100635 set_target_proxy(&s->target, s->be);
Willy Tarreau664beb82011-03-10 11:38:29 +0100636 }
637 else {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200638 err = SRV_STATUS_NOSRV;
639 goto out;
640 }
641
642 s->flags |= SN_ASSIGNED;
643 err = SRV_STATUS_OK;
644 out:
645
646 /* Either we take back our connection slot, or we offer it to someone
647 * else if we don't need it anymore.
648 */
649 if (conn_slot) {
Willy Tarreau827aee92011-03-10 16:55:02 +0100650 if (conn_slot == srv) {
651 sess_change_server(s, srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200652 } else {
653 if (may_dequeue_tasks(conn_slot, s->be))
654 process_srv_queue(conn_slot);
655 }
656 }
657
658 out_err:
659 return err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200660}
661
662
663/*
664 * This function assigns a server address to a session, and sets SN_ADDR_SET.
665 * The address is taken from the currently assigned server, or from the
666 * dispatch or transparent address.
667 *
668 * It may return :
669 * SRV_STATUS_OK if everything is OK.
670 * SRV_STATUS_INTERNAL for other unrecoverable errors.
671 *
672 * Upon successful return, the session flag SN_ADDR_SET is set. This flag is
673 * not cleared, so it's to the caller to clear it if required.
674 *
675 */
676int assign_server_address(struct session *s)
677{
678#ifdef DEBUG_FULL
679 fprintf(stderr,"assign_server_address : s=%p\n",s);
680#endif
681
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200682 if ((s->flags & SN_DIRECT) || (s->be->lbprm.algo & BE_LB_KIND)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200683 /* A server is necessarily known for this session */
684 if (!(s->flags & SN_ASSIGNED))
685 return SRV_STATUS_INTERNAL;
686
Willy Tarreau6471afb2011-09-23 10:54:59 +0200687 s->req->cons->addr.to = target_srv(&s->target)->addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200688
Willy Tarreau6471afb2011-09-23 10:54:59 +0200689 if (!is_addr(&s->req->cons->addr.to)) {
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200690 /* if the server has no address, we use the same address
691 * the client asked, which is handy for remapping ports
692 * locally on multiple addresses at once.
693 */
Willy Tarreau9b061e32012-04-07 18:03:52 +0200694 if (!(s->be->options & PR_O_TRANSP))
695 stream_sock_get_to_addr(s->req->prod);
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200696
Willy Tarreau6471afb2011-09-23 10:54:59 +0200697 if (s->req->prod->addr.to.ss_family == AF_INET) {
698 ((struct sockaddr_in *)&s->req->cons->addr.to)->sin_addr = ((struct sockaddr_in *)&s->req->prod->addr.to)->sin_addr;
699 } else if (s->req->prod->addr.to.ss_family == AF_INET6) {
700 ((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 +0200701 }
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200702 }
703
Willy Tarreaubaaee002006-06-26 02:48:02 +0200704 /* if this server remaps proxied ports, we'll use
705 * the port the client connected to with an offset. */
Willy Tarreau827aee92011-03-10 16:55:02 +0100706 if (target_srv(&s->target)->state & SRV_MAPPORTS) {
David du Colombier6f5ccb12011-03-10 22:26:24 +0100707 int base_port;
708
Willy Tarreau9b061e32012-04-07 18:03:52 +0200709 if (!(s->be->options & PR_O_TRANSP))
710 stream_sock_get_to_addr(s->req->prod);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100711
712 /* First, retrieve the port from the incoming connection */
Willy Tarreau6471afb2011-09-23 10:54:59 +0200713 base_port = get_host_port(&s->req->prod->addr.to);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100714
715 /* Second, assign the outgoing connection's port */
Willy Tarreau6471afb2011-09-23 10:54:59 +0200716 base_port += get_host_port(&s->req->cons->addr.to);
717 set_host_port(&s->req->cons->addr.to, base_port);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200718 }
719 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200720 else if (s->be->options & PR_O_DISPATCH) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200721 /* connect to the defined dispatch addr */
Willy Tarreau6471afb2011-09-23 10:54:59 +0200722 s->req->cons->addr.to = s->be->dispatch_addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200723 }
Willy Tarreau4b1f8592008-12-23 23:13:55 +0100724 else if (s->be->options & PR_O_TRANSP) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200725 /* in transparent mode, use the original dest addr if no dispatch specified */
Willy Tarreau9b061e32012-04-07 18:03:52 +0200726 stream_sock_get_to_addr(s->req->prod);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200727
Willy Tarreau6471afb2011-09-23 10:54:59 +0200728 if (s->req->prod->addr.to.ss_family == AF_INET || s->req->prod->addr.to.ss_family == AF_INET6) {
729 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 +0200730 }
Willy Tarreaubd414282008-01-19 13:46:35 +0100731 /* when we support IPv6 on the backend, we may add other tests */
732 //qfprintf(stderr, "Cannot get original server address.\n");
733 //return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200734 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100735 else if (s->be->options & PR_O_HTTP_PROXY) {
736 /* If HTTP PROXY option is set, then server is already assigned
737 * during incoming client request parsing. */
738 }
Willy Tarreau1a1158b2007-01-20 11:07:46 +0100739 else {
740 /* no server and no LB algorithm ! */
741 return SRV_STATUS_INTERNAL;
742 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200743
744 s->flags |= SN_ADDR_SET;
745 return SRV_STATUS_OK;
746}
747
748
749/* This function assigns a server to session <s> if required, and can add the
750 * connection to either the assigned server's queue or to the proxy's queue.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200751 * If ->srv_conn is set, the session is first released from the server.
752 * It may also be called with SN_DIRECT and/or SN_ASSIGNED though. It will
753 * be called before any connection and after any retry or redispatch occurs.
754 *
755 * It is not allowed to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200756 *
757 * Returns :
758 *
759 * SRV_STATUS_OK if everything is OK.
Willy Tarreau827aee92011-03-10 16:55:02 +0100760 * SRV_STATUS_NOSRV if no server is available. target_srv(&s->target) = NULL.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200761 * SRV_STATUS_QUEUED if the connection has been queued.
762 * SRV_STATUS_FULL if the server(s) is/are saturated and the
Willy Tarreau827aee92011-03-10 16:55:02 +0100763 * connection could not be queued at the server's,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200764 * which may be NULL if we queue on the backend.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200765 * SRV_STATUS_INTERNAL for other unrecoverable errors.
766 *
767 */
768int assign_server_and_queue(struct session *s)
769{
770 struct pendconn *p;
Willy Tarreau827aee92011-03-10 16:55:02 +0100771 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200772 int err;
773
774 if (s->pend_pos)
775 return SRV_STATUS_INTERNAL;
776
Willy Tarreau7c669d72008-06-20 15:04:11 +0200777 err = SRV_STATUS_OK;
778 if (!(s->flags & SN_ASSIGNED)) {
Willy Tarreau827aee92011-03-10 16:55:02 +0100779 struct server *prev_srv = target_srv(&s->target);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100780
Willy Tarreau7c669d72008-06-20 15:04:11 +0200781 err = assign_server(s);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100782 if (prev_srv) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200783 /* This session was previously assigned to a server. We have to
784 * update the session's and the server's stats :
785 * - if the server changed :
786 * - set TX_CK_DOWN if txn.flags was TX_CK_VALID
787 * - set SN_REDISP if it was successfully redispatched
788 * - increment srv->redispatches and be->redispatches
789 * - if the server remained the same : update retries.
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100790 */
791
Willy Tarreau827aee92011-03-10 16:55:02 +0100792 if (prev_srv != target_srv(&s->target)) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200793 if ((s->txn.flags & TX_CK_MASK) == TX_CK_VALID) {
794 s->txn.flags &= ~TX_CK_MASK;
795 s->txn.flags |= TX_CK_DOWN;
796 }
797 s->flags |= SN_REDISP;
Willy Tarreau3d80d912011-03-10 11:42:13 +0100798 prev_srv->counters.redispatches++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100799 s->be->be_counters.redispatches++;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200800 } else {
Willy Tarreau3d80d912011-03-10 11:42:13 +0100801 prev_srv->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100802 s->be->be_counters.retries++;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100803 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100804 }
805 }
806
Willy Tarreaubaaee002006-06-26 02:48:02 +0200807 switch (err) {
808 case SRV_STATUS_OK:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200809 /* we have SN_ASSIGNED set */
Willy Tarreau827aee92011-03-10 16:55:02 +0100810 srv = target_srv(&s->target);
811 if (!srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200812 return SRV_STATUS_OK; /* dispatch or proxy mode */
813
814 /* If we already have a connection slot, no need to check any queue */
Willy Tarreau827aee92011-03-10 16:55:02 +0100815 if (s->srv_conn == srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200816 return SRV_STATUS_OK;
817
818 /* OK, this session already has an assigned server, but no
819 * connection slot yet. Either it is a redispatch, or it was
820 * assigned from persistence information (direct mode).
821 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100822 if ((s->flags & SN_REDIRECTABLE) && srv->rdr_len) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200823 /* server scheduled for redirection, and already assigned. We
824 * don't want to go further nor check the queue.
Willy Tarreau21d2af32008-02-14 20:25:24 +0100825 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100826 sess_change_server(s, srv); /* not really needed in fact */
Willy Tarreau21d2af32008-02-14 20:25:24 +0100827 return SRV_STATUS_OK;
828 }
829
Willy Tarreau7c669d72008-06-20 15:04:11 +0200830 /* We might have to queue this session if the assigned server is full.
831 * We know we have to queue it into the server's queue, so if a maxqueue
832 * is set on the server, we must also check that the server's queue is
833 * not full, in which case we have to return FULL.
834 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100835 if (srv->maxconn &&
836 (srv->nbpend || srv->served >= srv_dynamic_maxconn(srv))) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200837
Willy Tarreau827aee92011-03-10 16:55:02 +0100838 if (srv->maxqueue > 0 && srv->nbpend >= srv->maxqueue)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200839 return SRV_STATUS_FULL;
840
Willy Tarreaubaaee002006-06-26 02:48:02 +0200841 p = pendconn_add(s);
842 if (p)
843 return SRV_STATUS_QUEUED;
844 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200845 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200846 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200847
848 /* OK, we can use this server. Let's reserve our place */
Willy Tarreau827aee92011-03-10 16:55:02 +0100849 sess_change_server(s, srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200850 return SRV_STATUS_OK;
851
852 case SRV_STATUS_FULL:
853 /* queue this session into the proxy's queue */
854 p = pendconn_add(s);
855 if (p)
856 return SRV_STATUS_QUEUED;
857 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200858 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200859
860 case SRV_STATUS_NOSRV:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200861 return err;
862
Willy Tarreaubaaee002006-06-26 02:48:02 +0200863 case SRV_STATUS_INTERNAL:
864 return err;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200865
Willy Tarreaubaaee002006-06-26 02:48:02 +0200866 default:
867 return SRV_STATUS_INTERNAL;
868 }
Willy Tarreau5b6995c2008-01-13 16:31:17 +0100869}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200870
Willy Tarreaub1d67742010-03-29 19:36:59 +0200871/* If an explicit source binding is specified on the server and/or backend, and
872 * this source makes use of the transparent proxy, then it is extracted now and
Willy Tarreau6471afb2011-09-23 10:54:59 +0200873 * assigned to the session's req->cons->addr.from entry.
Willy Tarreaub1d67742010-03-29 19:36:59 +0200874 */
875static void assign_tproxy_address(struct session *s)
876{
877#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY)
Willy Tarreau827aee92011-03-10 16:55:02 +0100878 struct server *srv = target_srv(&s->target);
879
880 if (srv && srv->state & SRV_BIND_SRC) {
881 switch (srv->state & SRV_TPROXY_MASK) {
Willy Tarreaub1d67742010-03-29 19:36:59 +0200882 case SRV_TPROXY_ADDR:
Willy Tarreau6471afb2011-09-23 10:54:59 +0200883 s->req->cons->addr.from = srv->tproxy_addr;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200884 break;
885 case SRV_TPROXY_CLI:
886 case SRV_TPROXY_CIP:
Emeric Brunec810d12010-10-22 16:36:33 +0200887 /* FIXME: what can we do if the client connects in IPv6 or unix socket ? */
Willy Tarreau6471afb2011-09-23 10:54:59 +0200888 s->req->cons->addr.from = s->req->prod->addr.from;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200889 break;
Willy Tarreaubce70882009-09-07 11:51:47 +0200890 case SRV_TPROXY_DYN:
Willy Tarreau827aee92011-03-10 16:55:02 +0100891 if (srv->bind_hdr_occ) {
Willy Tarreau294c4732011-12-16 21:35:50 +0100892 char *vptr;
893 int vlen;
894
Willy Tarreaubce70882009-09-07 11:51:47 +0200895 /* bind to the IP in a header */
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100896 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_family = AF_INET;
Willy Tarreau6471afb2011-09-23 10:54:59 +0200897 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_port = 0;
Willy Tarreau294c4732011-12-16 21:35:50 +0100898 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_addr.s_addr = 0;
899
900 if (http_get_hdr(&s->txn.req, srv->bind_hdr_name, srv->bind_hdr_len,
901 &s->txn.hdr_idx, srv->bind_hdr_occ, NULL, &vptr, &vlen)) {
902 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_addr.s_addr =
903 htonl(inetaddr_host_lim(vptr, vptr + vlen));
904 }
Willy Tarreaubce70882009-09-07 11:51:47 +0200905 }
906 break;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200907 default:
Willy Tarreau6471afb2011-09-23 10:54:59 +0200908 memset(&s->req->cons->addr.from, 0, sizeof(s->req->cons->addr.from));
Willy Tarreaub1d67742010-03-29 19:36:59 +0200909 }
910 }
911 else if (s->be->options & PR_O_BIND_SRC) {
912 switch (s->be->options & PR_O_TPXY_MASK) {
913 case PR_O_TPXY_ADDR:
Willy Tarreau6471afb2011-09-23 10:54:59 +0200914 s->req->cons->addr.from = s->be->tproxy_addr;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200915 break;
916 case PR_O_TPXY_CLI:
917 case PR_O_TPXY_CIP:
Emeric Brunec810d12010-10-22 16:36:33 +0200918 /* FIXME: what can we do if the client connects in IPv6 or socket unix? */
Willy Tarreau6471afb2011-09-23 10:54:59 +0200919 s->req->cons->addr.from = s->req->prod->addr.from;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200920 break;
Willy Tarreaubce70882009-09-07 11:51:47 +0200921 case PR_O_TPXY_DYN:
922 if (s->be->bind_hdr_occ) {
Willy Tarreau294c4732011-12-16 21:35:50 +0100923 char *vptr;
924 int vlen;
925
Willy Tarreaubce70882009-09-07 11:51:47 +0200926 /* bind to the IP in a header */
Willy Tarreau5dc1e982011-12-16 21:25:11 +0100927 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_family = AF_INET;
Willy Tarreau6471afb2011-09-23 10:54:59 +0200928 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_port = 0;
Willy Tarreau294c4732011-12-16 21:35:50 +0100929 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_addr.s_addr = 0;
930
931 if (http_get_hdr(&s->txn.req, s->be->bind_hdr_name, s->be->bind_hdr_len,
932 &s->txn.hdr_idx, s->be->bind_hdr_occ, NULL, &vptr, &vlen)) {
933 ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_addr.s_addr =
934 htonl(inetaddr_host_lim(vptr, vptr + vlen));
935 }
Willy Tarreaubce70882009-09-07 11:51:47 +0200936 }
937 break;
Willy Tarreaub1d67742010-03-29 19:36:59 +0200938 default:
Willy Tarreau6471afb2011-09-23 10:54:59 +0200939 memset(&s->req->cons->addr.from, 0, sizeof(s->req->cons->addr.from));
Willy Tarreaub1d67742010-03-29 19:36:59 +0200940 }
941 }
942#endif
943}
944
945
Willy Tarreaubaaee002006-06-26 02:48:02 +0200946/*
947 * This function initiates a connection to the server assigned to this session
Willy Tarreau6471afb2011-09-23 10:54:59 +0200948 * (s->target, s->req->cons->addr.to). It will assign a server if none
Willy Tarreau664beb82011-03-10 11:38:29 +0100949 * is assigned yet.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200950 * It can return one of :
951 * - SN_ERR_NONE if everything's OK
952 * - SN_ERR_SRVTO if there are no more servers
953 * - SN_ERR_SRVCL if the connection was refused by the server
954 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
955 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
956 * - SN_ERR_INTERNAL for any other purely internal errors
957 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
958 */
959int connect_server(struct session *s)
960{
Willy Tarreau827aee92011-03-10 16:55:02 +0100961 struct server *srv;
Willy Tarreau9650f372009-08-16 14:02:45 +0200962 int err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200963
964 if (!(s->flags & SN_ADDR_SET)) {
965 err = assign_server_address(s);
966 if (err != SRV_STATUS_OK)
967 return SN_ERR_INTERNAL;
968 }
969
Willy Tarreaua8f55d52010-05-31 17:44:19 +0200970 /* Prepare the stream interface for a TCP connection. Later
971 * we may assign a protocol-specific connect() function.
Willy Tarreau664beb82011-03-10 11:38:29 +0100972 * NOTE: when we later support HTTP keep-alive, we'll have to
973 * decide here if we can reuse the connection by comparing the
974 * session's freshly assigned target with the stream interface's.
Willy Tarreaua8f55d52010-05-31 17:44:19 +0200975 */
976 stream_sock_prepare_interface(s->req->cons);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100977 s->req->cons->connect = tcp_connect_server;
Willy Tarreau9b061e32012-04-07 18:03:52 +0200978 s->req->cons->get_src = getsockname;
979 s->req->cons->get_dst = getpeername;
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200980 /* the target was only on the session, assign it to the SI now */
Willy Tarreau9e000c62011-03-10 14:03:36 +0100981 copy_target(&s->req->cons->target, &s->target);
Willy Tarreaud88edf22009-06-14 15:48:17 +0200982
Willy Tarreau5ab04ec2011-03-20 10:32:26 +0100983 /* process the case where the server requires the PROXY protocol to be sent */
984 s->req->cons->send_proxy_ofs = 0;
985 if (s->target.type == TARG_TYPE_SERVER && (s->target.ptr.s->state & SRV_SEND_PROXY)) {
986 s->req->cons->send_proxy_ofs = 1; /* must compute size */
Willy Tarreau9b061e32012-04-07 18:03:52 +0200987 stream_sock_get_to_addr(s->req->prod);
Willy Tarreau5ab04ec2011-03-20 10:32:26 +0100988 }
989
Willy Tarreaub1d67742010-03-29 19:36:59 +0200990 assign_tproxy_address(s);
991
William Lallemandb7ff6a32012-03-02 14:35:21 +0100992 /* flag for logging source ip/port */
993 if (s->fe->options2 & PR_O2_SRC_ADDR)
994 s->req->cons->flags |= SI_FL_SRC_ADDR;
995
Willy Tarreauac825402011-03-04 22:04:29 +0100996 err = s->req->cons->connect(s->req->cons);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200997
Willy Tarreau9650f372009-08-16 14:02:45 +0200998 if (err != SN_ERR_NONE)
999 return err;
Willy Tarreau788e2842008-08-26 13:25:39 +02001000
Willy Tarreau827aee92011-03-10 16:55:02 +01001001 srv = target_srv(&s->target);
1002 if (srv) {
Willy Tarreau1e62de62008-11-11 20:20:02 +01001003 s->flags |= SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001004 srv->cur_sess++;
1005 if (srv->cur_sess > srv->counters.cur_sess_max)
1006 srv->counters.cur_sess_max = srv->cur_sess;
Willy Tarreau51406232008-03-10 22:04:20 +01001007 if (s->be->lbprm.server_take_conn)
Willy Tarreau827aee92011-03-10 16:55:02 +01001008 s->be->lbprm.server_take_conn(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001009 }
1010
Willy Tarreaubaaee002006-06-26 02:48:02 +02001011 return SN_ERR_NONE; /* connection is OK */
1012}
1013
1014
Willy Tarreaubaaee002006-06-26 02:48:02 +02001015/* This function performs the "redispatch" part of a connection attempt. It
1016 * will assign a server if required, queue the connection if required, and
1017 * handle errors that might arise at this level. It can change the server
1018 * state. It will return 1 if it encounters an error, switches the server
1019 * state, or has to queue a connection. Otherwise, it will return 0 indicating
1020 * that the connection is ready to use.
1021 */
1022
1023int srv_redispatch_connect(struct session *t)
1024{
Willy Tarreau827aee92011-03-10 16:55:02 +01001025 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001026 int conn_err;
1027
1028 /* We know that we don't have any connection pending, so we will
1029 * try to get a new one, and wait in this state if it's queued
1030 */
Willy Tarreau7c669d72008-06-20 15:04:11 +02001031 redispatch:
Willy Tarreaubaaee002006-06-26 02:48:02 +02001032 conn_err = assign_server_and_queue(t);
Willy Tarreau827aee92011-03-10 16:55:02 +01001033 srv = target_srv(&t->target);
1034
Willy Tarreaubaaee002006-06-26 02:48:02 +02001035 switch (conn_err) {
1036 case SRV_STATUS_OK:
1037 break;
1038
Willy Tarreau7c669d72008-06-20 15:04:11 +02001039 case SRV_STATUS_FULL:
1040 /* The server has reached its maxqueue limit. Either PR_O_REDISP is set
1041 * and we can redispatch to another server, or it is not and we return
1042 * 503. This only makes sense in DIRECT mode however, because normal LB
1043 * algorithms would never select such a server, and hash algorithms
Willy Tarreau827aee92011-03-10 16:55:02 +01001044 * would bring us on the same server again. Note that t->target is set
1045 * in this case.
Willy Tarreau7c669d72008-06-20 15:04:11 +02001046 */
Willy Tarreau4de91492010-01-22 19:10:05 +01001047 if (((t->flags & (SN_DIRECT|SN_FORCE_PRST)) == SN_DIRECT) &&
1048 (t->be->options & PR_O_REDISP)) {
Willy Tarreau7c669d72008-06-20 15:04:11 +02001049 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Willy Tarreau7c669d72008-06-20 15:04:11 +02001050 goto redispatch;
1051 }
1052
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001053 if (!t->req->cons->err_type) {
1054 t->req->cons->err_type = SI_ET_QUEUE_ERR;
Willy Tarreau827aee92011-03-10 16:55:02 +01001055 t->req->cons->err_loc = srv;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001056 }
Willy Tarreau7c669d72008-06-20 15:04:11 +02001057
Willy Tarreau827aee92011-03-10 16:55:02 +01001058 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001059 t->be->be_counters.failed_conns++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02001060 return 1;
1061
Willy Tarreaubaaee002006-06-26 02:48:02 +02001062 case SRV_STATUS_NOSRV:
Willy Tarreau827aee92011-03-10 16:55:02 +01001063 /* note: it is guaranteed that srv == NULL here */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001064 if (!t->req->cons->err_type) {
1065 t->req->cons->err_type = SI_ET_CONN_ERR;
1066 t->req->cons->err_loc = NULL;
1067 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +01001068
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001069 t->be->be_counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001070 return 1;
1071
1072 case SRV_STATUS_QUEUED:
Willy Tarreau35374672008-09-03 18:11:02 +02001073 t->req->cons->exp = tick_add_ifset(now_ms, t->be->timeout.queue);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001074 t->req->cons->state = SI_ST_QUE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001075 /* do nothing else and do not wake any other session up */
1076 return 1;
1077
Willy Tarreaubaaee002006-06-26 02:48:02 +02001078 case SRV_STATUS_INTERNAL:
1079 default:
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001080 if (!t->req->cons->err_type) {
1081 t->req->cons->err_type = SI_ET_CONN_OTHER;
Willy Tarreau827aee92011-03-10 16:55:02 +01001082 t->req->cons->err_loc = srv;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001083 }
1084
Willy Tarreau827aee92011-03-10 16:55:02 +01001085 if (srv)
1086 srv_inc_sess_ctr(srv);
1087 if (srv)
1088 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001089 t->be->be_counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001090
1091 /* release other sessions waiting for this server */
Willy Tarreau827aee92011-03-10 16:55:02 +01001092 if (may_dequeue_tasks(srv, t->be))
1093 process_srv_queue(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001094 return 1;
1095 }
1096 /* if we get here, it's because we got SRV_STATUS_OK, which also
1097 * means that the connection has not been queued.
1098 */
1099 return 0;
1100}
1101
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001102/* Apply RDP cookie persistence to the current session. For this, the function
1103 * tries to extract an RDP cookie from the request buffer, and look for the
1104 * matching server in the list. If the server is found, it is assigned to the
1105 * session. This always returns 1, and the analyser removes itself from the
1106 * list. Nothing is performed if a server was already assigned.
1107 */
1108int tcp_persist_rdp_cookie(struct session *s, struct buffer *req, int an_bit)
1109{
1110 struct proxy *px = s->be;
1111 int ret;
Willy Tarreau37406352012-04-23 16:16:37 +02001112 struct sample smp;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001113 struct server *srv = px->srv;
1114 struct sockaddr_in addr;
1115 char *p;
Willy Tarreau34db1082012-04-19 17:16:54 +02001116 struct arg args[2];
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001117
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001118 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 +02001119 now_ms, __FUNCTION__,
1120 s,
1121 req,
1122 req->rex, req->wex,
1123 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001124 req->i,
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001125 req->analysers);
1126
1127 if (s->flags & SN_ASSIGNED)
1128 goto no_cookie;
1129
Willy Tarreau37406352012-04-23 16:16:37 +02001130 memset(&smp, 0, sizeof(smp));
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001131
Willy Tarreau34db1082012-04-19 17:16:54 +02001132 args[0].type = ARGT_STR;
1133 args[0].data.str.str = s->be->rdp_cookie_name;
1134 args[0].data.str.len = s->be->rdp_cookie_len;
1135 args[1].type = ARGT_STOP;
1136
Willy Tarreau32389b72012-04-23 23:13:20 +02001137 ret = smp_fetch_rdp_cookie(px, s, NULL, ACL_DIR_REQ, args, &smp);
Willy Tarreauf853c462012-04-23 18:53:56 +02001138 if (ret == 0 || (smp.flags & SMP_F_MAY_CHANGE) || smp.data.str.len == 0)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001139 goto no_cookie;
1140
1141 memset(&addr, 0, sizeof(addr));
1142 addr.sin_family = AF_INET;
1143
Willy Tarreau664092c2011-12-16 19:11:42 +01001144 /* Considering an rdp cookie detected using acl, str ended with <cr><lf> and should return */
Willy Tarreauf853c462012-04-23 18:53:56 +02001145 addr.sin_addr.s_addr = strtoul(smp.data.str.str, &p, 10);
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001146 if (*p != '.')
1147 goto no_cookie;
1148 p++;
1149 addr.sin_port = (unsigned short)strtoul(p, &p, 10);
1150 if (*p != '.')
1151 goto no_cookie;
1152
Willy Tarreau9e000c62011-03-10 14:03:36 +01001153 clear_target(&s->target);
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001154 while (srv) {
1155 if (memcmp(&addr, &(srv->addr), sizeof(addr)) == 0) {
1156 if ((srv->state & SRV_RUNNING) || (px->options & PR_O_PERSIST)) {
1157 /* we found the server and it is usable */
1158 s->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01001159 set_target_server(&s->target, srv);
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001160 break;
1161 }
1162 }
1163 srv = srv->next;
1164 }
1165
1166no_cookie:
1167 req->analysers &= ~an_bit;
1168 req->analyse_exp = TICK_ETERNITY;
1169 return 1;
1170}
1171
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001172int be_downtime(struct proxy *px) {
Willy Tarreaub625a082007-11-26 01:15:43 +01001173 if (px->lbprm.tot_weight && px->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001174 return px->down_time;
1175
1176 return now.tv_sec - px->last_change + px->down_time;
1177}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001178
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001179/*
1180 * This function returns a string containing the balancing
1181 * mode of the proxy in a format suitable for stats.
1182 */
1183
1184const char *backend_lb_algo_str(int algo) {
1185
1186 if (algo == BE_LB_ALGO_RR)
1187 return "roundrobin";
1188 else if (algo == BE_LB_ALGO_SRR)
1189 return "static-rr";
Willy Tarreauf09c6602012-02-13 17:12:08 +01001190 else if (algo == BE_LB_ALGO_FAS)
1191 return "first";
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001192 else if (algo == BE_LB_ALGO_LC)
1193 return "leastconn";
1194 else if (algo == BE_LB_ALGO_SH)
1195 return "source";
1196 else if (algo == BE_LB_ALGO_UH)
1197 return "uri";
1198 else if (algo == BE_LB_ALGO_PH)
1199 return "url_param";
1200 else if (algo == BE_LB_ALGO_HH)
1201 return "hdr";
1202 else if (algo == BE_LB_ALGO_RCH)
1203 return "rdp-cookie";
1204 else
1205 return NULL;
1206}
1207
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001208/* This function parses a "balance" statement in a backend section describing
1209 * <curproxy>. It returns -1 if there is any error, otherwise zero. If it
1210 * returns -1, it may write an error message into ther <err> buffer, for at
1211 * most <errlen> bytes, trailing zero included. The trailing '\n' will not be
1212 * written. The function must be called with <args> pointing to the first word
1213 * after "balance".
1214 */
1215int backend_parse_balance(const char **args, char *err, int errlen, struct proxy *curproxy)
1216{
1217 if (!*(args[0])) {
1218 /* if no option is set, use round-robin by default */
Willy Tarreau31682232007-11-29 15:38:04 +01001219 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1220 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001221 return 0;
1222 }
1223
1224 if (!strcmp(args[0], "roundrobin")) {
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 }
Willy Tarreau9757a382009-10-03 12:56:50 +02001228 else if (!strcmp(args[0], "static-rr")) {
1229 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1230 curproxy->lbprm.algo |= BE_LB_ALGO_SRR;
1231 }
Willy Tarreauf09c6602012-02-13 17:12:08 +01001232 else if (!strcmp(args[0], "first")) {
1233 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1234 curproxy->lbprm.algo |= BE_LB_ALGO_FAS;
1235 }
Willy Tarreau51406232008-03-10 22:04:20 +01001236 else if (!strcmp(args[0], "leastconn")) {
1237 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1238 curproxy->lbprm.algo |= BE_LB_ALGO_LC;
1239 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001240 else if (!strcmp(args[0], "source")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001241 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1242 curproxy->lbprm.algo |= BE_LB_ALGO_SH;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001243 }
1244 else if (!strcmp(args[0], "uri")) {
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001245 int arg = 1;
1246
Willy Tarreau31682232007-11-29 15:38:04 +01001247 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1248 curproxy->lbprm.algo |= BE_LB_ALGO_UH;
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001249
1250 while (*args[arg]) {
1251 if (!strcmp(args[arg], "len")) {
1252 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
1253 snprintf(err, errlen, "'balance uri len' expects a positive integer (got '%s').", args[arg+1]);
1254 return -1;
1255 }
1256 curproxy->uri_len_limit = atoi(args[arg+1]);
1257 arg += 2;
1258 }
1259 else if (!strcmp(args[arg], "depth")) {
1260 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
1261 snprintf(err, errlen, "'balance uri depth' expects a positive integer (got '%s').", args[arg+1]);
1262 return -1;
1263 }
1264 /* hint: we store the position of the ending '/' (depth+1) so
1265 * that we avoid a comparison while computing the hash.
1266 */
1267 curproxy->uri_dirs_depth1 = atoi(args[arg+1]) + 1;
1268 arg += 2;
1269 }
1270 else {
1271 snprintf(err, errlen, "'balance uri' only accepts parameters 'len' and 'depth' (got '%s').", args[arg]);
1272 return -1;
1273 }
1274 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001275 }
Willy Tarreau01732802007-11-01 22:48:15 +01001276 else if (!strcmp(args[0], "url_param")) {
1277 if (!*args[1]) {
1278 snprintf(err, errlen, "'balance url_param' requires an URL parameter name.");
1279 return -1;
1280 }
Willy Tarreau31682232007-11-29 15:38:04 +01001281 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1282 curproxy->lbprm.algo |= BE_LB_ALGO_PH;
Willy Tarreaua534fea2008-08-03 12:19:50 +02001283
1284 free(curproxy->url_param_name);
Willy Tarreau01732802007-11-01 22:48:15 +01001285 curproxy->url_param_name = strdup(args[1]);
Willy Tarreaua534fea2008-08-03 12:19:50 +02001286 curproxy->url_param_len = strlen(args[1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001287 if (*args[2]) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001288 if (strcmp(args[2], "check_post")) {
1289 snprintf(err, errlen, "'balance url_param' only accepts check_post modifier.");
1290 return -1;
1291 }
1292 if (*args[3]) {
1293 /* TODO: maybe issue a warning if there is no value, no digits or too long */
1294 curproxy->url_param_post_limit = str2ui(args[3]);
1295 }
1296 /* if no limit, or faul value in args[3], then default to a moderate wordlen */
1297 if (!curproxy->url_param_post_limit)
1298 curproxy->url_param_post_limit = 48;
1299 else if ( curproxy->url_param_post_limit < 3 )
1300 curproxy->url_param_post_limit = 3; /* minimum example: S=3 or \r\nS=6& */
1301 }
Benoitaffb4812009-03-25 13:02:10 +01001302 }
1303 else if (!strncmp(args[0], "hdr(", 4)) {
1304 const char *beg, *end;
1305
1306 beg = args[0] + 4;
1307 end = strchr(beg, ')');
1308
1309 if (!end || end == beg) {
1310 snprintf(err, errlen, "'balance hdr(name)' requires an http header field name.");
1311 return -1;
1312 }
1313
1314 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1315 curproxy->lbprm.algo |= BE_LB_ALGO_HH;
1316
1317 free(curproxy->hh_name);
1318 curproxy->hh_len = end - beg;
1319 curproxy->hh_name = my_strndup(beg, end - beg);
1320 curproxy->hh_match_domain = 0;
1321
1322 if (*args[1]) {
1323 if (strcmp(args[1], "use_domain_only")) {
1324 snprintf(err, errlen, "'balance hdr(name)' only accepts 'use_domain_only' modifier.");
1325 return -1;
1326 }
1327 curproxy->hh_match_domain = 1;
1328 }
1329
Emeric Brun736aa232009-06-30 17:56:00 +02001330 }
1331 else if (!strncmp(args[0], "rdp-cookie", 10)) {
1332 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1333 curproxy->lbprm.algo |= BE_LB_ALGO_RCH;
1334
1335 if ( *(args[0] + 10 ) == '(' ) { /* cookie name */
1336 const char *beg, *end;
1337
1338 beg = args[0] + 11;
1339 end = strchr(beg, ')');
1340
1341 if (!end || end == beg) {
1342 snprintf(err, errlen, "'balance rdp-cookie(name)' requires an rdp cookie name.");
1343 return -1;
1344 }
1345
1346 free(curproxy->hh_name);
1347 curproxy->hh_name = my_strndup(beg, end - beg);
1348 curproxy->hh_len = end - beg;
1349 }
1350 else if ( *(args[0] + 10 ) == '\0' ) { /* default cookie name 'mstshash' */
1351 free(curproxy->hh_name);
1352 curproxy->hh_name = strdup("mstshash");
1353 curproxy->hh_len = strlen(curproxy->hh_name);
1354 }
1355 else { /* syntax */
1356 snprintf(err, errlen, "'balance rdp-cookie(name)' requires an rdp cookie name.");
1357 return -1;
1358 }
Willy Tarreau01732802007-11-01 22:48:15 +01001359 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001360 else {
Willy Tarreau9757a382009-10-03 12:56:50 +02001361 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 +01001362 return -1;
1363 }
1364 return 0;
1365}
1366
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001367
1368/************************************************************************/
1369/* All supported keywords must be declared here. */
1370/************************************************************************/
1371
Willy Tarreau34db1082012-04-19 17:16:54 +02001372/* set temp integer to the number of enabled servers on the proxy.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001373 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001374 * undefined behaviour.
1375 */
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001376static int
1377acl_fetch_nbsrv(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001378 const struct arg *args, struct sample *smp)
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001379{
Willy Tarreau37406352012-04-23 16:16:37 +02001380 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001381 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001382 px = args->data.prx;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001383
1384 if (px->srv_act)
Willy Tarreauf853c462012-04-23 18:53:56 +02001385 smp->data.uint = px->srv_act;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001386 else if (px->lbprm.fbck)
Willy Tarreauf853c462012-04-23 18:53:56 +02001387 smp->data.uint = 1;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001388 else
Willy Tarreauf853c462012-04-23 18:53:56 +02001389 smp->data.uint = px->srv_bck;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001390
1391 return 1;
1392}
1393
Willy Tarreau37406352012-04-23 16:16:37 +02001394/* report in smp->flags a success or failure depending on the designated
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001395 * server's state. There is no match function involved since there's no pattern.
Willy Tarreau34db1082012-04-19 17:16:54 +02001396 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1397 * undefined behaviour.
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001398 */
1399static int
1400acl_fetch_srv_is_up(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001401 const struct arg *args, struct sample *smp)
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001402{
Willy Tarreau24e32d82012-04-23 23:55:44 +02001403 struct server *srv = args->data.srv;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001404
Willy Tarreau37406352012-04-23 16:16:37 +02001405 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001406 smp->type = SMP_T_BOOL;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001407 if (!(srv->state & SRV_MAINTAIN) &&
1408 (!(srv->state & SRV_CHECKED) || (srv->state & SRV_RUNNING)))
Willy Tarreau197e10a2012-04-23 19:18:42 +02001409 smp->data.uint = 1;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001410 else
Willy Tarreau197e10a2012-04-23 19:18:42 +02001411 smp->data.uint = 0;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001412 return 1;
1413}
1414
Willy Tarreau34db1082012-04-19 17:16:54 +02001415/* set temp integer to the number of enabled servers on the proxy.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001416 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001417 * undefined behaviour.
1418 */
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001419static int
1420acl_fetch_connslots(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001421 const struct arg *args, struct sample *smp)
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001422{
1423 struct server *iterator;
Willy Tarreaud28c3532012-04-19 19:28:33 +02001424
Willy Tarreau37406352012-04-23 16:16:37 +02001425 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001426 smp->type = SMP_T_UINT;
1427 smp->data.uint = 0;
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001428
Willy Tarreau24e32d82012-04-23 23:55:44 +02001429 for (iterator = args->data.prx->srv; iterator; iterator = iterator->next) {
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001430 if ((iterator->state & SRV_RUNNING) == 0)
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001431 continue;
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001432
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001433 if (iterator->maxconn == 0 || iterator->maxqueue == 0) {
Willy Tarreaua5e37562011-12-16 17:06:15 +01001434 /* configuration is stupid */
Willy Tarreauf853c462012-04-23 18:53:56 +02001435 smp->data.uint = -1; /* FIXME: stupid value! */
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001436 return 1;
1437 }
1438
Willy Tarreauf853c462012-04-23 18:53:56 +02001439 smp->data.uint += (iterator->maxconn - iterator->cur_sess)
Willy Tarreau422aa072012-04-20 20:49:27 +02001440 + (iterator->maxqueue - iterator->nbpend);
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001441 }
1442
1443 return 1;
1444}
1445
Willy Tarreaua5e37562011-12-16 17:06:15 +01001446/* set temp integer to the id of the backend */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001447static int
1448acl_fetch_be_id(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001449 const struct arg *args, struct sample *smp)
Willy Tarreau37406352012-04-23 16:16:37 +02001450{
Willy Tarreauf853c462012-04-23 18:53:56 +02001451 smp->flags = SMP_F_VOL_TXN;
1452 smp->type = SMP_T_UINT;
1453 smp->data.uint = l4->be->uuid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001454 return 1;
1455}
1456
Willy Tarreaua5e37562011-12-16 17:06:15 +01001457/* set temp integer to the id of the server */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001458static int
1459acl_fetch_srv_id(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001460 const struct arg *args, struct sample *smp)
Willy Tarreau37406352012-04-23 16:16:37 +02001461{
Willy Tarreau827aee92011-03-10 16:55:02 +01001462 if (!target_srv(&l4->target))
Willy Tarreau17af4192011-02-23 14:27:06 +01001463 return 0;
1464
Willy Tarreauf853c462012-04-23 18:53:56 +02001465 smp->type = SMP_T_UINT;
1466 smp->data.uint = target_srv(&l4->target)->puid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001467
1468 return 1;
1469}
1470
Willy Tarreau34db1082012-04-19 17:16:54 +02001471/* set temp integer to the number of connections per second reaching the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001472 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001473 * undefined behaviour.
1474 */
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001475static int
1476acl_fetch_be_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001477 const struct arg *args, struct sample *smp)
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001478{
Willy Tarreau37406352012-04-23 16:16:37 +02001479 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001480 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001481 smp->data.uint = read_freq_ctr(&args->data.prx->be_sess_per_sec);
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001482 return 1;
1483}
1484
Willy Tarreau34db1082012-04-19 17:16:54 +02001485/* set temp integer to the number of concurrent connections on the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001486 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001487 * undefined behaviour.
1488 */
Willy Tarreaua36af912009-10-10 12:02:45 +02001489static int
1490acl_fetch_be_conn(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001491 const struct arg *args, struct sample *smp)
Willy Tarreaua36af912009-10-10 12:02:45 +02001492{
Willy Tarreau37406352012-04-23 16:16:37 +02001493 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001494 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001495 smp->data.uint = args->data.prx->beconn;
Willy Tarreaua36af912009-10-10 12:02:45 +02001496 return 1;
1497}
1498
Willy Tarreau34db1082012-04-19 17:16:54 +02001499/* set temp integer to the total number of queued connections on the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001500 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001501 * undefined behaviour.
1502 */
Willy Tarreaua36af912009-10-10 12:02:45 +02001503static int
1504acl_fetch_queue_size(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001505 const struct arg *args, struct sample *smp)
Willy Tarreaua36af912009-10-10 12:02:45 +02001506{
Willy Tarreau37406352012-04-23 16:16:37 +02001507 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001508 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001509 smp->data.uint = args->data.prx->totpend;
Willy Tarreaua36af912009-10-10 12:02:45 +02001510 return 1;
1511}
1512
Willy Tarreaua5e37562011-12-16 17:06:15 +01001513/* set temp integer to the total number of queued connections on the backend divided
Willy Tarreaua36af912009-10-10 12:02:45 +02001514 * by the number of running servers and rounded up. If there is no running
1515 * server, we return twice the total, just as if we had half a running server.
1516 * This is more or less correct anyway, since we expect the last server to come
1517 * back soon.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001518 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001519 * undefined behaviour.
Willy Tarreaua36af912009-10-10 12:02:45 +02001520 */
1521static int
1522acl_fetch_avg_queue_size(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001523 const struct arg *args, struct sample *smp)
Willy Tarreaua36af912009-10-10 12:02:45 +02001524{
1525 int nbsrv;
1526
Willy Tarreau37406352012-04-23 16:16:37 +02001527 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001528 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001529 px = args->data.prx;
Willy Tarreaua36af912009-10-10 12:02:45 +02001530
1531 if (px->srv_act)
1532 nbsrv = px->srv_act;
1533 else if (px->lbprm.fbck)
1534 nbsrv = 1;
1535 else
1536 nbsrv = px->srv_bck;
1537
1538 if (nbsrv > 0)
Willy Tarreauf853c462012-04-23 18:53:56 +02001539 smp->data.uint = (px->totpend + nbsrv - 1) / nbsrv;
Willy Tarreaua36af912009-10-10 12:02:45 +02001540 else
Willy Tarreauf853c462012-04-23 18:53:56 +02001541 smp->data.uint = px->totpend * 2;
Willy Tarreaua36af912009-10-10 12:02:45 +02001542
1543 return 1;
1544}
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001545
Willy Tarreau34db1082012-04-19 17:16:54 +02001546/* set temp integer to the number of concurrent connections on the server in the backend.
1547 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1548 * undefined behaviour.
1549 */
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001550static int
1551acl_fetch_srv_conn(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau24e32d82012-04-23 23:55:44 +02001552 const struct arg *args, struct sample *smp)
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001553{
Willy Tarreauf853c462012-04-23 18:53:56 +02001554 smp->flags = SMP_F_VOL_TEST;
1555 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001556 smp->data.uint = args->data.srv->cur_sess;
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001557 return 1;
1558}
1559
Willy Tarreau61612d42012-04-19 18:42:05 +02001560/* Note: must not be declared <const> as its list will be overwritten.
1561 * Please take care of keeping this list alphabetically sorted.
1562 */
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001563static struct acl_kw_list acl_kws = {{ },{
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001564 { "avg_queue", acl_parse_int, acl_fetch_avg_queue_size, acl_match_int, ACL_USE_NOTHING, ARG1(1,BE) },
1565 { "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 +02001566 { "be_id", acl_parse_int, acl_fetch_be_id, acl_match_int, ACL_USE_NOTHING, 0 },
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02001567 { "be_sess_rate", acl_parse_int, acl_fetch_be_sess_rate, acl_match_int, ACL_USE_NOTHING, ARG1(1,BE) },
1568 { "connslots", acl_parse_int, acl_fetch_connslots, acl_match_int, ACL_USE_NOTHING, ARG1(1,BE) },
1569 { "nbsrv", acl_parse_int, acl_fetch_nbsrv, acl_match_int, ACL_USE_NOTHING, ARG1(1,BE) },
1570 { "queue", acl_parse_int, acl_fetch_queue_size, acl_match_int, ACL_USE_NOTHING, ARG1(1,BE) },
Willy Tarreau61612d42012-04-19 18:42:05 +02001571 { "srv_conn", acl_parse_int, acl_fetch_srv_conn, acl_match_int, ACL_USE_NOTHING, ARG1(1,SRV) },
1572 { "srv_id", acl_parse_int, acl_fetch_srv_id, acl_match_int, ACL_USE_RTR_INTERNAL, 0 },
1573 { "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 +01001574 { NULL, NULL, NULL, NULL },
1575}};
1576
1577
1578__attribute__((constructor))
1579static void __backend_init(void)
1580{
1581 acl_register_keywords(&acl_kws);
1582}
1583
Willy Tarreaubaaee002006-06-26 02:48:02 +02001584/*
1585 * Local variables:
1586 * c-indent-level: 8
1587 * c-basic-offset: 8
1588 * End:
1589 */