blob: ae3e2b1c2efdcaa3e670d27afdddfe2b749d0e77 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Backend variables and functions.
3 *
Willy Tarreau1a7eca12013-01-07 22:38:03 +01004 * Copyright 2000-2013 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 Tarreauc7e42382012-08-24 19:22:53 +020022#include <common/buffer.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020023#include <common/compat.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020024#include <common/config.h>
Willy Tarreau7c669d72008-06-20 15:04:11 +020025#include <common/debug.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020026#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020027#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020028
Willy Tarreaubaaee002006-06-26 02:48:02 +020029#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020030
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +010031#include <proto/acl.h>
Willy Tarreau34db1082012-04-19 17:16:54 +020032#include <proto/arg.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020033#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020034#include <proto/channel.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020035#include <proto/frontend.h>
Willy Tarreau6b2e11b2009-10-01 07:52:15 +020036#include <proto/lb_chash.h>
Willy Tarreauf09c6602012-02-13 17:12:08 +010037#include <proto/lb_fas.h>
Willy Tarreauf89c1872009-10-01 11:19:37 +020038#include <proto/lb_fwlc.h>
39#include <proto/lb_fwrr.h>
40#include <proto/lb_map.h>
Willy Tarreau3fdb3662012-11-12 00:42:33 +010041#include <proto/obj_type.h>
Willy Tarreaud4c33c82013-01-07 21:59:07 +010042#include <proto/payload.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020043#include <proto/protocol.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044#include <proto/proto_http.h>
Willy Tarreaua8f55d52010-05-31 17:44:19 +020045#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020046#include <proto/queue.h>
Willy Tarreaud4c33c82013-01-07 21:59:07 +010047#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010048#include <proto/server.h>
Willy Tarreau7c669d72008-06-20 15:04:11 +020049#include <proto/session.h>
Willy Tarreau75bf2c92012-08-20 17:01:35 +020050#include <proto/raw_sock.h>
Willy Tarreau9e000c62011-03-10 14:03:36 +010051#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020052#include <proto/task.h>
53
Willy Tarreaubaaee002006-06-26 02:48:02 +020054/*
55 * This function recounts the number of usable active and backup servers for
56 * proxy <p>. These numbers are returned into the p->srv_act and p->srv_bck.
Willy Tarreaub625a082007-11-26 01:15:43 +010057 * This function also recomputes the total active and backup weights. However,
Willy Tarreauf4cca452008-03-08 21:42:54 +010058 * it does not update tot_weight nor tot_used. Use update_backend_weight() for
Willy Tarreaub625a082007-11-26 01:15:43 +010059 * this.
Willy Tarreaubaaee002006-06-26 02:48:02 +020060 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +020061void recount_servers(struct proxy *px)
Willy Tarreaubaaee002006-06-26 02:48:02 +020062{
63 struct server *srv;
64
Willy Tarreau20697042007-11-15 23:26:18 +010065 px->srv_act = px->srv_bck = 0;
66 px->lbprm.tot_wact = px->lbprm.tot_wbck = 0;
Willy Tarreaub625a082007-11-26 01:15:43 +010067 px->lbprm.fbck = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +020068 for (srv = px->srv; srv != NULL; srv = srv->next) {
Willy Tarreaub625a082007-11-26 01:15:43 +010069 if (!srv_is_usable(srv->state, srv->eweight))
70 continue;
71
72 if (srv->state & SRV_BACKUP) {
73 if (!px->srv_bck &&
Willy Tarreauf4cca452008-03-08 21:42:54 +010074 !(px->options & PR_O_USE_ALL_BK))
Willy Tarreaub625a082007-11-26 01:15:43 +010075 px->lbprm.fbck = srv;
76 px->srv_bck++;
77 px->lbprm.tot_wbck += srv->eweight;
78 } else {
79 px->srv_act++;
80 px->lbprm.tot_wact += srv->eweight;
Willy Tarreaubaaee002006-06-26 02:48:02 +020081 }
82 }
Willy Tarreaub625a082007-11-26 01:15:43 +010083}
Willy Tarreau20697042007-11-15 23:26:18 +010084
Willy Tarreaub625a082007-11-26 01:15:43 +010085/* This function simply updates the backend's tot_weight and tot_used values
86 * after servers weights have been updated. It is designed to be used after
87 * recount_servers() or equivalent.
88 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +020089void update_backend_weight(struct proxy *px)
Willy Tarreaub625a082007-11-26 01:15:43 +010090{
Willy Tarreau20697042007-11-15 23:26:18 +010091 if (px->srv_act) {
92 px->lbprm.tot_weight = px->lbprm.tot_wact;
93 px->lbprm.tot_used = px->srv_act;
94 }
Willy Tarreaub625a082007-11-26 01:15:43 +010095 else if (px->lbprm.fbck) {
96 /* use only the first backup server */
97 px->lbprm.tot_weight = px->lbprm.fbck->eweight;
98 px->lbprm.tot_used = 1;
Willy Tarreau20697042007-11-15 23:26:18 +010099 }
100 else {
Willy Tarreaub625a082007-11-26 01:15:43 +0100101 px->lbprm.tot_weight = px->lbprm.tot_wbck;
102 px->lbprm.tot_used = px->srv_bck;
Willy Tarreau20697042007-11-15 23:26:18 +0100103 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100104}
105
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200106/*
107 * This function tries to find a running server for the proxy <px> following
108 * the source hash method. Depending on the number of active/backup servers,
109 * it will either look for active servers, or for backup servers.
110 * If any server is found, it will be returned. If no valid server is found,
111 * NULL is returned.
112 */
113struct server *get_server_sh(struct proxy *px, const char *addr, int len)
114{
115 unsigned int h, l;
116
117 if (px->lbprm.tot_weight == 0)
118 return NULL;
119
120 l = h = 0;
121
122 /* note: we won't hash if there's only one server left */
123 if (px->lbprm.tot_used == 1)
124 goto hash_done;
125
126 while ((l + sizeof (int)) <= len) {
127 h ^= ntohl(*(unsigned int *)(&addr[l]));
128 l += sizeof (int);
129 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100130 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
131 h = full_hash(h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200132 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200133 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
134 return chash_get_server_hash(px, h);
135 else
136 return map_get_server_hash(px, h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200137}
138
139/*
140 * This function tries to find a running server for the proxy <px> following
141 * the URI hash method. In order to optimize cache hits, the hash computation
142 * ends at the question mark. Depending on the number of active/backup servers,
143 * it will either look for active servers, or for backup servers.
144 * If any server is found, it will be returned. If no valid server is found,
145 * NULL is returned.
146 *
147 * This code was contributed by Guillaume Dallaire, who also selected this hash
148 * algorithm out of a tens because it gave him the best results.
149 *
150 */
151struct server *get_server_uh(struct proxy *px, char *uri, int uri_len)
152{
153 unsigned long hash = 0;
154 int c;
155 int slashes = 0;
156
157 if (px->lbprm.tot_weight == 0)
158 return NULL;
159
160 /* note: we won't hash if there's only one server left */
161 if (px->lbprm.tot_used == 1)
162 goto hash_done;
163
164 if (px->uri_len_limit)
165 uri_len = MIN(uri_len, px->uri_len_limit);
166
167 while (uri_len--) {
168 c = *uri++;
169 if (c == '/') {
170 slashes++;
171 if (slashes == px->uri_dirs_depth1) /* depth+1 */
172 break;
173 }
Oskar Stolc8dc41842012-05-19 10:19:54 +0100174 else if (c == '?' && !px->uri_whole)
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200175 break;
176
177 hash = c + (hash << 6) + (hash << 16) - hash;
178 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100179 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
180 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200181 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200182 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
183 return chash_get_server_hash(px, hash);
184 else
185 return map_get_server_hash(px, hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200186}
187
Willy Tarreau01732802007-11-01 22:48:15 +0100188/*
189 * This function tries to find a running server for the proxy <px> following
190 * the URL parameter hash method. It looks for a specific parameter in the
191 * URL and hashes it to compute the server ID. This is useful to optimize
192 * performance by avoiding bounces between servers in contexts where sessions
193 * are shared but cookies are not usable. If the parameter is not found, NULL
194 * is returned. If any server is found, it will be returned. If no valid server
195 * is found, NULL is returned.
Willy Tarreau01732802007-11-01 22:48:15 +0100196 */
197struct server *get_server_ph(struct proxy *px, const char *uri, int uri_len)
198{
199 unsigned long hash = 0;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200200 const char *p;
201 const char *params;
Willy Tarreau01732802007-11-01 22:48:15 +0100202 int plen;
203
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200204 /* when tot_weight is 0 then so is srv_count */
Willy Tarreau20697042007-11-15 23:26:18 +0100205 if (px->lbprm.tot_weight == 0)
Willy Tarreau01732802007-11-01 22:48:15 +0100206 return NULL;
207
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200208 if ((p = memchr(uri, '?', uri_len)) == NULL)
209 return NULL;
210
Willy Tarreau01732802007-11-01 22:48:15 +0100211 p++;
212
213 uri_len -= (p - uri);
214 plen = px->url_param_len;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200215 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100216
217 while (uri_len > plen) {
218 /* Look for the parameter name followed by an equal symbol */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200219 if (params[plen] == '=') {
220 if (memcmp(params, px->url_param_name, plen) == 0) {
221 /* OK, we have the parameter here at <params>, and
Willy Tarreau01732802007-11-01 22:48:15 +0100222 * the value after the equal sign, at <p>
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200223 * skip the equal symbol
Willy Tarreau01732802007-11-01 22:48:15 +0100224 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200225 p += plen + 1;
226 uri_len -= plen + 1;
227
Willy Tarreau01732802007-11-01 22:48:15 +0100228 while (uri_len && *p != '&') {
229 hash = *p + (hash << 6) + (hash << 16) - hash;
230 uri_len--;
231 p++;
232 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100233 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
234 hash = full_hash(hash);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200235 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
236 return chash_get_server_hash(px, hash);
237 else
238 return map_get_server_hash(px, hash);
Willy Tarreau01732802007-11-01 22:48:15 +0100239 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200240 }
241 /* skip to next parameter */
242 p = memchr(params, '&', uri_len);
243 if (!p)
244 return NULL;
245 p++;
246 uri_len -= (p - params);
247 params = p;
248 }
249 return NULL;
250}
251
252/*
253 * this does the same as the previous server_ph, but check the body contents
254 */
255struct server *get_server_ph_post(struct session *s)
256{
257 unsigned long hash = 0;
258 struct http_txn *txn = &s->txn;
Willy Tarreau7421efb2012-07-02 15:11:27 +0200259 struct channel *req = s->req;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200260 struct http_msg *msg = &txn->req;
261 struct proxy *px = s->be;
262 unsigned int plen = px->url_param_len;
Willy Tarreau124d9912011-03-01 20:30:48 +0100263 unsigned long len = msg->body_len;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200264 const char *params = b_ptr(req->buf, (int)(msg->sov - req->buf->o));
Willy Tarreau157dd632009-12-06 19:18:09 +0100265 const char *p = params;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200266
Willy Tarreau9b28e032012-10-12 23:49:43 +0200267 if (len > buffer_len(req->buf) - msg->sov)
268 len = buffer_len(req->buf) - msg->sov;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200269
Willy Tarreau157dd632009-12-06 19:18:09 +0100270 if (len == 0)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200271 return NULL;
272
Willy Tarreau157dd632009-12-06 19:18:09 +0100273 if (px->lbprm.tot_weight == 0)
274 return NULL;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200275
276 while (len > plen) {
277 /* Look for the parameter name followed by an equal symbol */
278 if (params[plen] == '=') {
279 if (memcmp(params, px->url_param_name, plen) == 0) {
280 /* OK, we have the parameter here at <params>, and
281 * the value after the equal sign, at <p>
282 * skip the equal symbol
283 */
284 p += plen + 1;
285 len -= plen + 1;
286
287 while (len && *p != '&') {
288 if (unlikely(!HTTP_IS_TOKEN(*p))) {
Willy Tarreau157dd632009-12-06 19:18:09 +0100289 /* if in a POST, body must be URI encoded or it's not a URI.
290 * Do not interprete any possible binary data as a parameter.
291 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200292 if (likely(HTTP_IS_LWS(*p))) /* eol, uncertain uri len */
293 break;
294 return NULL; /* oh, no; this is not uri-encoded.
295 * This body does not contain parameters.
296 */
297 }
298 hash = *p + (hash << 6) + (hash << 16) - hash;
299 len--;
300 p++;
301 /* should we break if vlen exceeds limit? */
302 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100303 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
304 hash = full_hash(hash);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200305 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
306 return chash_get_server_hash(px, hash);
307 else
308 return map_get_server_hash(px, hash);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200309 }
310 }
Willy Tarreau01732802007-11-01 22:48:15 +0100311 /* skip to next parameter */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200312 p = memchr(params, '&', len);
Willy Tarreau01732802007-11-01 22:48:15 +0100313 if (!p)
314 return NULL;
315 p++;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200316 len -= (p - params);
317 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100318 }
319 return NULL;
320}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200321
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200322
Willy Tarreaubaaee002006-06-26 02:48:02 +0200323/*
Benoitaffb4812009-03-25 13:02:10 +0100324 * This function tries to find a running server for the proxy <px> following
325 * the Header parameter hash method. It looks for a specific parameter in the
326 * URL and hashes it to compute the server ID. This is useful to optimize
327 * performance by avoiding bounces between servers in contexts where sessions
328 * are shared but cookies are not usable. If the parameter is not found, NULL
329 * is returned. If any server is found, it will be returned. If no valid server
330 * is found, NULL is returned.
331 */
332struct server *get_server_hh(struct session *s)
333{
334 unsigned long hash = 0;
335 struct http_txn *txn = &s->txn;
Benoitaffb4812009-03-25 13:02:10 +0100336 struct proxy *px = s->be;
337 unsigned int plen = px->hh_len;
338 unsigned long len;
339 struct hdr_ctx ctx;
340 const char *p;
341
342 /* tot_weight appears to mean srv_count */
343 if (px->lbprm.tot_weight == 0)
344 return NULL;
345
Benoitaffb4812009-03-25 13:02:10 +0100346 ctx.idx = 0;
347
348 /* if the message is chunked, we skip the chunk size, but use the value as len */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200349 http_find_header2(px->hh_name, plen, b_ptr(s->req->buf, (int)-s->req->buf->o), &txn->hdr_idx, &ctx);
Benoitaffb4812009-03-25 13:02:10 +0100350
351 /* if the header is not found or empty, let's fallback to round robin */
352 if (!ctx.idx || !ctx.vlen)
353 return NULL;
354
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200355 /* note: we won't hash if there's only one server left */
356 if (px->lbprm.tot_used == 1)
357 goto hash_done;
358
Benoitaffb4812009-03-25 13:02:10 +0100359 /* Found a the hh_name in the headers.
360 * we will compute the hash based on this value ctx.val.
361 */
362 len = ctx.vlen;
363 p = (char *)ctx.line + ctx.val;
364 if (!px->hh_match_domain) {
365 while (len) {
366 hash = *p + (hash << 6) + (hash << 16) - hash;
367 len--;
368 p++;
369 }
370 } else {
371 int dohash = 0;
372 p += len - 1;
373 /* special computation, use only main domain name, not tld/host
374 * going back from the end of string, start hashing at first
375 * dot stop at next.
376 * This is designed to work with the 'Host' header, and requires
377 * a special option to activate this.
378 */
379 while (len) {
380 if (*p == '.') {
381 if (!dohash)
382 dohash = 1;
383 else
384 break;
385 } else {
386 if (dohash)
387 hash = *p + (hash << 6) + (hash << 16) - hash;
388 }
389 len--;
390 p--;
391 }
392 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100393 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
394 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200395 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200396 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
397 return chash_get_server_hash(px, hash);
398 else
399 return map_get_server_hash(px, hash);
Benoitaffb4812009-03-25 13:02:10 +0100400}
401
Willy Tarreau34db1082012-04-19 17:16:54 +0200402/* RDP Cookie HASH. */
Emeric Brun736aa232009-06-30 17:56:00 +0200403struct server *get_server_rch(struct session *s)
404{
405 unsigned long hash = 0;
406 struct proxy *px = s->be;
407 unsigned long len;
408 const char *p;
409 int ret;
Willy Tarreau37406352012-04-23 16:16:37 +0200410 struct sample smp;
Willy Tarreau34db1082012-04-19 17:16:54 +0200411 struct arg args[2];
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200412 int rewind;
Emeric Brun736aa232009-06-30 17:56:00 +0200413
414 /* tot_weight appears to mean srv_count */
415 if (px->lbprm.tot_weight == 0)
416 return NULL;
417
Willy Tarreau37406352012-04-23 16:16:37 +0200418 memset(&smp, 0, sizeof(smp));
Emeric Brun736aa232009-06-30 17:56:00 +0200419
Willy Tarreau34db1082012-04-19 17:16:54 +0200420 args[0].type = ARGT_STR;
421 args[0].data.str.str = px->hh_name;
422 args[0].data.str.len = px->hh_len;
423 args[1].type = ARGT_STOP;
424
Willy Tarreau9b28e032012-10-12 23:49:43 +0200425 b_rew(s->req->buf, rewind = s->req->buf->o);
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200426
Willy Tarreauef38c392013-07-22 16:29:32 +0200427 ret = smp_fetch_rdp_cookie(px, s, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, args, &smp, NULL);
Willy Tarreauf853c462012-04-23 18:53:56 +0200428 len = smp.data.str.len;
Willy Tarreau664092c2011-12-16 19:11:42 +0100429
Willy Tarreau9b28e032012-10-12 23:49:43 +0200430 b_adv(s->req->buf, rewind);
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200431
Willy Tarreau37406352012-04-23 16:16:37 +0200432 if (ret == 0 || (smp.flags & SMP_F_MAY_CHANGE) || len == 0)
Emeric Brun736aa232009-06-30 17:56:00 +0200433 return NULL;
434
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200435 /* note: we won't hash if there's only one server left */
436 if (px->lbprm.tot_used == 1)
437 goto hash_done;
438
Emeric Brun736aa232009-06-30 17:56:00 +0200439 /* Found a the hh_name in the headers.
440 * we will compute the hash based on this value ctx.val.
441 */
Willy Tarreauf853c462012-04-23 18:53:56 +0200442 p = smp.data.str.str;
Emeric Brun736aa232009-06-30 17:56:00 +0200443 while (len) {
444 hash = *p + (hash << 6) + (hash << 16) - hash;
445 len--;
446 p++;
447 }
Willy Tarreau798a39c2010-11-24 15:04:29 +0100448 if ((px->lbprm.algo & BE_LB_HASH_TYPE) != BE_LB_HASH_MAP)
449 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200450 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200451 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
452 return chash_get_server_hash(px, hash);
453 else
454 return map_get_server_hash(px, hash);
Emeric Brun736aa232009-06-30 17:56:00 +0200455}
Benoitaffb4812009-03-25 13:02:10 +0100456
457/*
Willy Tarreau7c669d72008-06-20 15:04:11 +0200458 * This function applies the load-balancing algorithm to the session, as
459 * defined by the backend it is assigned to. The session is then marked as
460 * 'assigned'.
461 *
462 * This function MAY NOT be called with SN_ASSIGNED already set. If the session
463 * had a server previously assigned, it is rebalanced, trying to avoid the same
Willy Tarreau827aee92011-03-10 16:55:02 +0100464 * server, which should still be present in target_srv(&s->target) before the call.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200465 * The function tries to keep the original connection slot if it reconnects to
466 * the same server, otherwise it releases it and tries to offer it.
467 *
468 * It is illegal to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200469 *
470 * It may return :
Willy Tarreau664beb82011-03-10 11:38:29 +0100471 * SRV_STATUS_OK if everything is OK. ->srv and ->target are assigned.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200472 * SRV_STATUS_NOSRV if no server is available. Session is not ASSIGNED
473 * SRV_STATUS_FULL if all servers are saturated. Session is not ASSIGNED
Willy Tarreaubaaee002006-06-26 02:48:02 +0200474 * SRV_STATUS_INTERNAL for other unrecoverable errors.
475 *
Willy Tarreau7c669d72008-06-20 15:04:11 +0200476 * Upon successful return, the session flag SN_ASSIGNED is set to indicate that
Willy Tarreau827aee92011-03-10 16:55:02 +0100477 * it does not need to be called anymore. This means that target_srv(&s->target)
478 * can be trusted in balance and direct modes.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200479 *
480 */
481
482int assign_server(struct session *s)
483{
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100484
Willy Tarreau7c669d72008-06-20 15:04:11 +0200485 struct server *conn_slot;
Willy Tarreau827aee92011-03-10 16:55:02 +0100486 struct server *srv, *prev_srv;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200487 int err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100488
Simon Horman8effd3d2011-08-13 08:03:52 +0900489 DPRINTF(stderr,"assign_server : s=%p\n",s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200490
Willy Tarreau7c669d72008-06-20 15:04:11 +0200491 err = SRV_STATUS_INTERNAL;
492 if (unlikely(s->pend_pos || s->flags & SN_ASSIGNED))
493 goto out_err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100494
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100495 prev_srv = objt_server(s->target);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200496 conn_slot = s->srv_conn;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200497
Willy Tarreau7c669d72008-06-20 15:04:11 +0200498 /* We have to release any connection slot before applying any LB algo,
499 * otherwise we may erroneously end up with no available slot.
500 */
501 if (conn_slot)
502 sess_change_server(s, NULL);
503
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100504 /* We will now try to find the good server and store it into <objt_server(s->target)>.
505 * Note that <objt_server(s->target)> may be NULL in case of dispatch or proxy mode,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200506 * as well as if no server is available (check error code).
507 */
Willy Tarreau1a20a5d2007-11-01 21:08:19 +0100508
Willy Tarreau827aee92011-03-10 16:55:02 +0100509 srv = NULL;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100510 s->target = NULL;
Willy Tarreau664beb82011-03-10 11:38:29 +0100511
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200512 if (s->be->lbprm.algo & BE_LB_KIND) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200513 /* we must check if we have at least one server available */
514 if (!s->be->lbprm.tot_weight) {
515 err = SRV_STATUS_NOSRV;
516 goto out;
517 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200518
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200519 /* First check whether we need to fetch some data or simply call
520 * the LB lookup function. Only the hashing functions will need
521 * some input data in fact, and will support multiple algorithms.
522 */
523 switch (s->be->lbprm.algo & BE_LB_LKUP) {
524 case BE_LB_LKUP_RRTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100525 srv = fwrr_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200526 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200527
Willy Tarreauf09c6602012-02-13 17:12:08 +0100528 case BE_LB_LKUP_FSTREE:
529 srv = fas_get_next_server(s->be, prev_srv);
530 break;
531
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200532 case BE_LB_LKUP_LCTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100533 srv = fwlc_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200534 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200535
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200536 case BE_LB_LKUP_CHTREE:
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200537 case BE_LB_LKUP_MAP:
Willy Tarreau9757a382009-10-03 12:56:50 +0200538 if ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200539 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100540 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200541 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100542 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau9757a382009-10-03 12:56:50 +0200543 break;
544 }
545 else if ((s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_HI) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200546 /* unknown balancing algorithm */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200547 err = SRV_STATUS_INTERNAL;
548 goto out;
549 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200550
551 switch (s->be->lbprm.algo & BE_LB_PARM) {
552 case BE_LB_HASH_SRC:
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200553 if (s->req->prod->conn->addr.from.ss_family == AF_INET) {
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200554 srv = get_server_sh(s->be,
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200555 (void *)&((struct sockaddr_in *)&s->req->prod->conn->addr.from)->sin_addr,
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200556 4);
557 }
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200558 else if (s->req->prod->conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200559 srv = get_server_sh(s->be,
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200560 (void *)&((struct sockaddr_in6 *)&s->req->prod->conn->addr.from)->sin6_addr,
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200561 16);
562 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200563 else {
564 /* unknown IP family */
565 err = SRV_STATUS_INTERNAL;
566 goto out;
567 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200568 break;
569
570 case BE_LB_HASH_URI:
571 /* URI hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100572 if (s->txn.req.msg_state < HTTP_MSG_BODY)
573 break;
Willy Tarreau827aee92011-03-10 16:55:02 +0100574 srv = get_server_uh(s->be,
Willy Tarreau9b28e032012-10-12 23:49:43 +0200575 b_ptr(s->req->buf, (int)(s->txn.req.sl.rq.u - s->req->buf->o)),
Willy Tarreau827aee92011-03-10 16:55:02 +0100576 s->txn.req.sl.rq.u_l);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200577 break;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200578
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200579 case BE_LB_HASH_PRM:
580 /* URL Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100581 if (s->txn.req.msg_state < HTTP_MSG_BODY)
582 break;
Willy Tarreau61a21a32011-03-01 20:35:49 +0100583
Willy Tarreau827aee92011-03-10 16:55:02 +0100584 srv = get_server_ph(s->be,
Willy Tarreau9b28e032012-10-12 23:49:43 +0200585 b_ptr(s->req->buf, (int)(s->txn.req.sl.rq.u - s->req->buf->o)),
Willy Tarreau827aee92011-03-10 16:55:02 +0100586 s->txn.req.sl.rq.u_l);
Willy Tarreau61a21a32011-03-01 20:35:49 +0100587
Willy Tarreau827aee92011-03-10 16:55:02 +0100588 if (!srv && s->txn.meth == HTTP_METH_POST)
589 srv = get_server_ph_post(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200590 break;
Benoitaffb4812009-03-25 13:02:10 +0100591
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200592 case BE_LB_HASH_HDR:
593 /* Header Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100594 if (s->txn.req.msg_state < HTTP_MSG_BODY)
595 break;
Willy Tarreau827aee92011-03-10 16:55:02 +0100596 srv = get_server_hh(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200597 break;
598
599 case BE_LB_HASH_RDP:
600 /* RDP Cookie hashing */
Willy Tarreau827aee92011-03-10 16:55:02 +0100601 srv = get_server_rch(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200602 break;
603
604 default:
605 /* unknown balancing algorithm */
606 err = SRV_STATUS_INTERNAL;
607 goto out;
Benoitaffb4812009-03-25 13:02:10 +0100608 }
Emeric Brun736aa232009-06-30 17:56:00 +0200609
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200610 /* If the hashing parameter was not found, let's fall
611 * back to round robin on the map.
612 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100613 if (!srv) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200614 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100615 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200616 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100617 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200618 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200619
620 /* end of map-based LB */
Emeric Brun736aa232009-06-30 17:56:00 +0200621 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200622
Willy Tarreau7c669d72008-06-20 15:04:11 +0200623 default:
624 /* unknown balancing algorithm */
625 err = SRV_STATUS_INTERNAL;
626 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200627 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200628
Willy Tarreau827aee92011-03-10 16:55:02 +0100629 if (!srv) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200630 err = SRV_STATUS_FULL;
631 goto out;
632 }
Willy Tarreau827aee92011-03-10 16:55:02 +0100633 else if (srv != prev_srv) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100634 s->be->be_counters.cum_lbconn++;
Willy Tarreau827aee92011-03-10 16:55:02 +0100635 srv->counters.cum_lbconn++;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100636 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100637 s->target = &srv->obj_type;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200638 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200639 else if (s->be->options & (PR_O_DISPATCH | PR_O_TRANSP)) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100640 s->target = &s->be->obj_type;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200641 }
David du Colombier6f5ccb12011-03-10 22:26:24 +0100642 else if ((s->be->options & PR_O_HTTP_PROXY) &&
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200643 is_addr(&s->req->cons->conn->addr.to)) {
Willy Tarreau664beb82011-03-10 11:38:29 +0100644 /* in proxy mode, we need a valid destination address */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100645 s->target = &s->be->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +0100646 }
647 else {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200648 err = SRV_STATUS_NOSRV;
649 goto out;
650 }
651
652 s->flags |= SN_ASSIGNED;
653 err = SRV_STATUS_OK;
654 out:
655
656 /* Either we take back our connection slot, or we offer it to someone
657 * else if we don't need it anymore.
658 */
659 if (conn_slot) {
Willy Tarreau827aee92011-03-10 16:55:02 +0100660 if (conn_slot == srv) {
661 sess_change_server(s, srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200662 } else {
663 if (may_dequeue_tasks(conn_slot, s->be))
664 process_srv_queue(conn_slot);
665 }
666 }
667
668 out_err:
669 return err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200670}
671
672
673/*
674 * This function assigns a server address to a session, and sets SN_ADDR_SET.
675 * The address is taken from the currently assigned server, or from the
676 * dispatch or transparent address.
677 *
678 * It may return :
679 * SRV_STATUS_OK if everything is OK.
680 * SRV_STATUS_INTERNAL for other unrecoverable errors.
681 *
682 * Upon successful return, the session flag SN_ADDR_SET is set. This flag is
683 * not cleared, so it's to the caller to clear it if required.
684 *
685 */
686int assign_server_address(struct session *s)
687{
688#ifdef DEBUG_FULL
689 fprintf(stderr,"assign_server_address : s=%p\n",s);
690#endif
691
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200692 if ((s->flags & SN_DIRECT) || (s->be->lbprm.algo & BE_LB_KIND)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200693 /* A server is necessarily known for this session */
694 if (!(s->flags & SN_ASSIGNED))
695 return SRV_STATUS_INTERNAL;
696
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100697 s->req->cons->conn->addr.to = objt_server(s->target)->addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200698
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200699 if (!is_addr(&s->req->cons->conn->addr.to)) {
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200700 /* if the server has no address, we use the same address
701 * the client asked, which is handy for remapping ports
702 * locally on multiple addresses at once.
703 */
Willy Tarreau9b061e32012-04-07 18:03:52 +0200704 if (!(s->be->options & PR_O_TRANSP))
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200705 conn_get_to_addr(s->req->prod->conn);
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200706
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200707 if (s->req->prod->conn->addr.to.ss_family == AF_INET) {
708 ((struct sockaddr_in *)&s->req->cons->conn->addr.to)->sin_addr = ((struct sockaddr_in *)&s->req->prod->conn->addr.to)->sin_addr;
709 } else if (s->req->prod->conn->addr.to.ss_family == AF_INET6) {
710 ((struct sockaddr_in6 *)&s->req->cons->conn->addr.to)->sin6_addr = ((struct sockaddr_in6 *)&s->req->prod->conn->addr.to)->sin6_addr;
Emeric Brunec810d12010-10-22 16:36:33 +0200711 }
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200712 }
713
Willy Tarreaubaaee002006-06-26 02:48:02 +0200714 /* if this server remaps proxied ports, we'll use
715 * the port the client connected to with an offset. */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100716 if (objt_server(s->target)->state & SRV_MAPPORTS) {
David du Colombier6f5ccb12011-03-10 22:26:24 +0100717 int base_port;
718
Willy Tarreau9b061e32012-04-07 18:03:52 +0200719 if (!(s->be->options & PR_O_TRANSP))
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200720 conn_get_to_addr(s->req->prod->conn);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100721
722 /* First, retrieve the port from the incoming connection */
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200723 base_port = get_host_port(&s->req->prod->conn->addr.to);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100724
725 /* Second, assign the outgoing connection's port */
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200726 base_port += get_host_port(&s->req->cons->conn->addr.to);
727 set_host_port(&s->req->cons->conn->addr.to, base_port);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200728 }
729 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200730 else if (s->be->options & PR_O_DISPATCH) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200731 /* connect to the defined dispatch addr */
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200732 s->req->cons->conn->addr.to = s->be->dispatch_addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200733 }
Willy Tarreau4b1f8592008-12-23 23:13:55 +0100734 else if (s->be->options & PR_O_TRANSP) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200735 /* in transparent mode, use the original dest addr if no dispatch specified */
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200736 conn_get_to_addr(s->req->prod->conn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200737
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200738 if (s->req->prod->conn->addr.to.ss_family == AF_INET || s->req->prod->conn->addr.to.ss_family == AF_INET6) {
739 memcpy(&s->req->cons->conn->addr.to, &s->req->prod->conn->addr.to, MIN(sizeof(s->req->cons->conn->addr.to), sizeof(s->req->prod->conn->addr.to)));
Emeric Brunec810d12010-10-22 16:36:33 +0200740 }
Willy Tarreaubd414282008-01-19 13:46:35 +0100741 /* when we support IPv6 on the backend, we may add other tests */
742 //qfprintf(stderr, "Cannot get original server address.\n");
743 //return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200744 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100745 else if (s->be->options & PR_O_HTTP_PROXY) {
746 /* If HTTP PROXY option is set, then server is already assigned
747 * during incoming client request parsing. */
748 }
Willy Tarreau1a1158b2007-01-20 11:07:46 +0100749 else {
750 /* no server and no LB algorithm ! */
751 return SRV_STATUS_INTERNAL;
752 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200753
754 s->flags |= SN_ADDR_SET;
755 return SRV_STATUS_OK;
756}
757
758
759/* This function assigns a server to session <s> if required, and can add the
760 * connection to either the assigned server's queue or to the proxy's queue.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200761 * If ->srv_conn is set, the session is first released from the server.
762 * It may also be called with SN_DIRECT and/or SN_ASSIGNED though. It will
763 * be called before any connection and after any retry or redispatch occurs.
764 *
765 * It is not allowed to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200766 *
767 * Returns :
768 *
769 * SRV_STATUS_OK if everything is OK.
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100770 * SRV_STATUS_NOSRV if no server is available. objt_server(s->target) = NULL.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200771 * SRV_STATUS_QUEUED if the connection has been queued.
772 * SRV_STATUS_FULL if the server(s) is/are saturated and the
Willy Tarreau827aee92011-03-10 16:55:02 +0100773 * connection could not be queued at the server's,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200774 * which may be NULL if we queue on the backend.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200775 * SRV_STATUS_INTERNAL for other unrecoverable errors.
776 *
777 */
778int assign_server_and_queue(struct session *s)
779{
780 struct pendconn *p;
Willy Tarreau827aee92011-03-10 16:55:02 +0100781 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200782 int err;
783
784 if (s->pend_pos)
785 return SRV_STATUS_INTERNAL;
786
Willy Tarreau7c669d72008-06-20 15:04:11 +0200787 err = SRV_STATUS_OK;
788 if (!(s->flags & SN_ASSIGNED)) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100789 struct server *prev_srv = objt_server(s->target);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100790
Willy Tarreau7c669d72008-06-20 15:04:11 +0200791 err = assign_server(s);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100792 if (prev_srv) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200793 /* This session was previously assigned to a server. We have to
794 * update the session's and the server's stats :
795 * - if the server changed :
796 * - set TX_CK_DOWN if txn.flags was TX_CK_VALID
797 * - set SN_REDISP if it was successfully redispatched
798 * - increment srv->redispatches and be->redispatches
799 * - if the server remained the same : update retries.
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100800 */
801
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100802 if (prev_srv != objt_server(s->target)) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200803 if ((s->txn.flags & TX_CK_MASK) == TX_CK_VALID) {
804 s->txn.flags &= ~TX_CK_MASK;
805 s->txn.flags |= TX_CK_DOWN;
806 }
807 s->flags |= SN_REDISP;
Willy Tarreau3d80d912011-03-10 11:42:13 +0100808 prev_srv->counters.redispatches++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100809 s->be->be_counters.redispatches++;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200810 } else {
Willy Tarreau3d80d912011-03-10 11:42:13 +0100811 prev_srv->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100812 s->be->be_counters.retries++;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100813 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100814 }
815 }
816
Willy Tarreaubaaee002006-06-26 02:48:02 +0200817 switch (err) {
818 case SRV_STATUS_OK:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200819 /* we have SN_ASSIGNED set */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100820 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100821 if (!srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200822 return SRV_STATUS_OK; /* dispatch or proxy mode */
823
824 /* If we already have a connection slot, no need to check any queue */
Willy Tarreau827aee92011-03-10 16:55:02 +0100825 if (s->srv_conn == srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200826 return SRV_STATUS_OK;
827
828 /* OK, this session already has an assigned server, but no
829 * connection slot yet. Either it is a redispatch, or it was
830 * assigned from persistence information (direct mode).
831 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100832 if ((s->flags & SN_REDIRECTABLE) && srv->rdr_len) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200833 /* server scheduled for redirection, and already assigned. We
834 * don't want to go further nor check the queue.
Willy Tarreau21d2af32008-02-14 20:25:24 +0100835 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100836 sess_change_server(s, srv); /* not really needed in fact */
Willy Tarreau21d2af32008-02-14 20:25:24 +0100837 return SRV_STATUS_OK;
838 }
839
Willy Tarreau7c669d72008-06-20 15:04:11 +0200840 /* We might have to queue this session if the assigned server is full.
841 * We know we have to queue it into the server's queue, so if a maxqueue
842 * is set on the server, we must also check that the server's queue is
843 * not full, in which case we have to return FULL.
844 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100845 if (srv->maxconn &&
846 (srv->nbpend || srv->served >= srv_dynamic_maxconn(srv))) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200847
Willy Tarreau827aee92011-03-10 16:55:02 +0100848 if (srv->maxqueue > 0 && srv->nbpend >= srv->maxqueue)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200849 return SRV_STATUS_FULL;
850
Willy Tarreaubaaee002006-06-26 02:48:02 +0200851 p = pendconn_add(s);
852 if (p)
853 return SRV_STATUS_QUEUED;
854 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200855 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200856 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200857
858 /* OK, we can use this server. Let's reserve our place */
Willy Tarreau827aee92011-03-10 16:55:02 +0100859 sess_change_server(s, srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200860 return SRV_STATUS_OK;
861
862 case SRV_STATUS_FULL:
863 /* queue this session into the proxy's queue */
864 p = pendconn_add(s);
865 if (p)
866 return SRV_STATUS_QUEUED;
867 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200868 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200869
870 case SRV_STATUS_NOSRV:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200871 return err;
872
Willy Tarreaubaaee002006-06-26 02:48:02 +0200873 case SRV_STATUS_INTERNAL:
874 return err;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200875
Willy Tarreaubaaee002006-06-26 02:48:02 +0200876 default:
877 return SRV_STATUS_INTERNAL;
878 }
Willy Tarreau5b6995c2008-01-13 16:31:17 +0100879}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200880
Willy Tarreaub1d67742010-03-29 19:36:59 +0200881/* If an explicit source binding is specified on the server and/or backend, and
882 * this source makes use of the transparent proxy, then it is extracted now and
Willy Tarreau6471afb2011-09-23 10:54:59 +0200883 * assigned to the session's req->cons->addr.from entry.
Willy Tarreaub1d67742010-03-29 19:36:59 +0200884 */
885static void assign_tproxy_address(struct session *s)
886{
Pieter Baauwd551fb52013-05-08 22:49:23 +0200887#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_TRANSPARENT)
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100888 struct server *srv = objt_server(s->target);
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100889 struct conn_src *src;
Willy Tarreau827aee92011-03-10 16:55:02 +0100890
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100891 if (srv && srv->conn_src.opts & CO_SRC_BIND)
892 src = &srv->conn_src;
893 else if (s->be->conn_src.opts & CO_SRC_BIND)
894 src = &s->be->conn_src;
895 else
896 return;
Willy Tarreau294c4732011-12-16 21:35:50 +0100897
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100898 switch (src->opts & CO_SRC_TPROXY_MASK) {
899 case CO_SRC_TPROXY_ADDR:
900 s->req->cons->conn->addr.from = src->tproxy_addr;
901 break;
902 case CO_SRC_TPROXY_CLI:
903 case CO_SRC_TPROXY_CIP:
904 /* FIXME: what can we do if the client connects in IPv6 or unix socket ? */
905 s->req->cons->conn->addr.from = s->req->prod->conn->addr.from;
906 break;
907 case CO_SRC_TPROXY_DYN:
908 if (src->bind_hdr_occ) {
909 char *vptr;
910 int vlen;
911 int rewind;
Willy Tarreau294c4732011-12-16 21:35:50 +0100912
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100913 /* bind to the IP in a header */
914 ((struct sockaddr_in *)&s->req->cons->conn->addr.from)->sin_family = AF_INET;
915 ((struct sockaddr_in *)&s->req->cons->conn->addr.from)->sin_port = 0;
916 ((struct sockaddr_in *)&s->req->cons->conn->addr.from)->sin_addr.s_addr = 0;
Willy Tarreau294c4732011-12-16 21:35:50 +0100917
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100918 b_rew(s->req->buf, rewind = s->req->buf->o);
919 if (http_get_hdr(&s->txn.req, src->bind_hdr_name, src->bind_hdr_len,
920 &s->txn.hdr_idx, src->bind_hdr_occ, NULL, &vptr, &vlen)) {
921 ((struct sockaddr_in *)&s->req->cons->conn->addr.from)->sin_addr.s_addr =
922 htonl(inetaddr_host_lim(vptr, vptr + vlen));
Willy Tarreaubce70882009-09-07 11:51:47 +0200923 }
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100924 b_adv(s->req->buf, rewind);
Willy Tarreaub1d67742010-03-29 19:36:59 +0200925 }
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100926 break;
927 default:
928 memset(&s->req->cons->conn->addr.from, 0, sizeof(s->req->cons->conn->addr.from));
Willy Tarreaub1d67742010-03-29 19:36:59 +0200929 }
930#endif
931}
932
933
Willy Tarreaubaaee002006-06-26 02:48:02 +0200934/*
935 * This function initiates a connection to the server assigned to this session
Willy Tarreau6471afb2011-09-23 10:54:59 +0200936 * (s->target, s->req->cons->addr.to). It will assign a server if none
Willy Tarreau664beb82011-03-10 11:38:29 +0100937 * is assigned yet.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200938 * It can return one of :
939 * - SN_ERR_NONE if everything's OK
940 * - SN_ERR_SRVTO if there are no more servers
941 * - SN_ERR_SRVCL if the connection was refused by the server
942 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
943 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
944 * - SN_ERR_INTERNAL for any other purely internal errors
945 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
946 */
947int connect_server(struct session *s)
948{
Willy Tarreau827aee92011-03-10 16:55:02 +0100949 struct server *srv;
Willy Tarreau9650f372009-08-16 14:02:45 +0200950 int err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200951
952 if (!(s->flags & SN_ADDR_SET)) {
953 err = assign_server_address(s);
954 if (err != SRV_STATUS_OK)
955 return SN_ERR_INTERNAL;
956 }
957
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200958 /* the target was only on the session, assign it to the SI now */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100959 s->req->cons->conn->target = s->target;
Willy Tarreaud88edf22009-06-14 15:48:17 +0200960
Willy Tarreau26d8c592012-05-07 18:12:14 +0200961 /* set the correct protocol on the output stream interface */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100962 if (objt_server(s->target)) {
963 si_prepare_conn(s->req->cons, objt_server(s->target)->proto, objt_server(s->target)->xprt);
Willy Tarreaud02394b2012-05-11 18:32:18 +0200964 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100965 else if (obj_type(s->target) == OBJ_TYPE_PROXY) {
Willy Tarreau75bf2c92012-08-20 17:01:35 +0200966 /* proxies exclusively run on raw_sock right now */
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200967 si_prepare_conn(s->req->cons, protocol_by_family(s->req->cons->conn->addr.to.ss_family), &raw_sock);
Willy Tarreau73b013b2012-05-21 16:31:45 +0200968 if (!si_ctrl(s->req->cons))
Willy Tarreau26d8c592012-05-07 18:12:14 +0200969 return SN_ERR_INTERNAL;
970 }
Willy Tarreaud02394b2012-05-11 18:32:18 +0200971 else
972 return SN_ERR_INTERNAL; /* how did we get there ? */
973
974 /* process the case where the server requires the PROXY protocol to be sent */
975 s->req->cons->send_proxy_ofs = 0;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100976 if (objt_server(s->target) && (objt_server(s->target)->state & SRV_SEND_PROXY)) {
Willy Tarreaud02394b2012-05-11 18:32:18 +0200977 s->req->cons->send_proxy_ofs = 1; /* must compute size */
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200978 conn_get_to_addr(s->req->prod->conn);
Willy Tarreaud02394b2012-05-11 18:32:18 +0200979 }
Willy Tarreau26d8c592012-05-07 18:12:14 +0200980
Willy Tarreaub1d67742010-03-29 19:36:59 +0200981 assign_tproxy_address(s);
982
William Lallemandb7ff6a32012-03-02 14:35:21 +0100983 /* flag for logging source ip/port */
984 if (s->fe->options2 & PR_O2_SRC_ADDR)
985 s->req->cons->flags |= SI_FL_SRC_ADDR;
986
Willy Tarreau14f8e862012-08-30 22:23:13 +0200987 /* disable lingering */
988 if (s->be->options & PR_O_TCP_NOLING)
989 s->req->cons->flags |= SI_FL_NOLINGER;
990
Willy Tarreau73b013b2012-05-21 16:31:45 +0200991 err = si_connect(s->req->cons);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200992
Willy Tarreau9650f372009-08-16 14:02:45 +0200993 if (err != SN_ERR_NONE)
994 return err;
Willy Tarreau788e2842008-08-26 13:25:39 +0200995
Willy Tarreau14f8e862012-08-30 22:23:13 +0200996 /* set connect timeout */
997 s->req->cons->exp = tick_add_ifset(now_ms, s->be->timeout.connect);
998
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100999 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001000 if (srv) {
Willy Tarreau1e62de62008-11-11 20:20:02 +01001001 s->flags |= SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001002 srv->cur_sess++;
1003 if (srv->cur_sess > srv->counters.cur_sess_max)
1004 srv->counters.cur_sess_max = srv->cur_sess;
Willy Tarreau51406232008-03-10 22:04:20 +01001005 if (s->be->lbprm.server_take_conn)
Willy Tarreau827aee92011-03-10 16:55:02 +01001006 s->be->lbprm.server_take_conn(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001007 }
1008
Willy Tarreaubaaee002006-06-26 02:48:02 +02001009 return SN_ERR_NONE; /* connection is OK */
1010}
1011
1012
Willy Tarreaubaaee002006-06-26 02:48:02 +02001013/* This function performs the "redispatch" part of a connection attempt. It
1014 * will assign a server if required, queue the connection if required, and
1015 * handle errors that might arise at this level. It can change the server
1016 * state. It will return 1 if it encounters an error, switches the server
1017 * state, or has to queue a connection. Otherwise, it will return 0 indicating
1018 * that the connection is ready to use.
1019 */
1020
1021int srv_redispatch_connect(struct session *t)
1022{
Willy Tarreau827aee92011-03-10 16:55:02 +01001023 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001024 int conn_err;
1025
1026 /* We know that we don't have any connection pending, so we will
1027 * try to get a new one, and wait in this state if it's queued
1028 */
Willy Tarreau7c669d72008-06-20 15:04:11 +02001029 redispatch:
Willy Tarreaubaaee002006-06-26 02:48:02 +02001030 conn_err = assign_server_and_queue(t);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001031 srv = objt_server(t->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001032
Willy Tarreaubaaee002006-06-26 02:48:02 +02001033 switch (conn_err) {
1034 case SRV_STATUS_OK:
1035 break;
1036
Willy Tarreau7c669d72008-06-20 15:04:11 +02001037 case SRV_STATUS_FULL:
1038 /* The server has reached its maxqueue limit. Either PR_O_REDISP is set
1039 * and we can redispatch to another server, or it is not and we return
1040 * 503. This only makes sense in DIRECT mode however, because normal LB
1041 * algorithms would never select such a server, and hash algorithms
Willy Tarreau827aee92011-03-10 16:55:02 +01001042 * would bring us on the same server again. Note that t->target is set
1043 * in this case.
Willy Tarreau7c669d72008-06-20 15:04:11 +02001044 */
Willy Tarreau4de91492010-01-22 19:10:05 +01001045 if (((t->flags & (SN_DIRECT|SN_FORCE_PRST)) == SN_DIRECT) &&
1046 (t->be->options & PR_O_REDISP)) {
Willy Tarreau7c669d72008-06-20 15:04:11 +02001047 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Willy Tarreau7c669d72008-06-20 15:04:11 +02001048 goto redispatch;
1049 }
1050
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001051 if (!t->req->cons->err_type) {
1052 t->req->cons->err_type = SI_ET_QUEUE_ERR;
Willy Tarreau827aee92011-03-10 16:55:02 +01001053 t->req->cons->err_loc = srv;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001054 }
Willy Tarreau7c669d72008-06-20 15:04:11 +02001055
Willy Tarreau827aee92011-03-10 16:55:02 +01001056 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001057 t->be->be_counters.failed_conns++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02001058 return 1;
1059
Willy Tarreaubaaee002006-06-26 02:48:02 +02001060 case SRV_STATUS_NOSRV:
Willy Tarreau827aee92011-03-10 16:55:02 +01001061 /* note: it is guaranteed that srv == NULL here */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001062 if (!t->req->cons->err_type) {
1063 t->req->cons->err_type = SI_ET_CONN_ERR;
1064 t->req->cons->err_loc = NULL;
1065 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +01001066
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001067 t->be->be_counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001068 return 1;
1069
1070 case SRV_STATUS_QUEUED:
Willy Tarreau35374672008-09-03 18:11:02 +02001071 t->req->cons->exp = tick_add_ifset(now_ms, t->be->timeout.queue);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001072 t->req->cons->state = SI_ST_QUE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001073 /* do nothing else and do not wake any other session up */
1074 return 1;
1075
Willy Tarreaubaaee002006-06-26 02:48:02 +02001076 case SRV_STATUS_INTERNAL:
1077 default:
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001078 if (!t->req->cons->err_type) {
1079 t->req->cons->err_type = SI_ET_CONN_OTHER;
Willy Tarreau827aee92011-03-10 16:55:02 +01001080 t->req->cons->err_loc = srv;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001081 }
1082
Willy Tarreau827aee92011-03-10 16:55:02 +01001083 if (srv)
1084 srv_inc_sess_ctr(srv);
1085 if (srv)
1086 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001087 t->be->be_counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001088
1089 /* release other sessions waiting for this server */
Willy Tarreau827aee92011-03-10 16:55:02 +01001090 if (may_dequeue_tasks(srv, t->be))
1091 process_srv_queue(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001092 return 1;
1093 }
1094 /* if we get here, it's because we got SRV_STATUS_OK, which also
1095 * means that the connection has not been queued.
1096 */
1097 return 0;
1098}
1099
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001100/* Apply RDP cookie persistence to the current session. For this, the function
1101 * tries to extract an RDP cookie from the request buffer, and look for the
1102 * matching server in the list. If the server is found, it is assigned to the
1103 * session. This always returns 1, and the analyser removes itself from the
1104 * list. Nothing is performed if a server was already assigned.
1105 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02001106int tcp_persist_rdp_cookie(struct session *s, struct channel *req, int an_bit)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001107{
1108 struct proxy *px = s->be;
1109 int ret;
Willy Tarreau37406352012-04-23 16:16:37 +02001110 struct sample smp;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001111 struct server *srv = px->srv;
1112 struct sockaddr_in addr;
1113 char *p;
Willy Tarreau34db1082012-04-19 17:16:54 +02001114 struct arg args[2];
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001115
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001116 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 +02001117 now_ms, __FUNCTION__,
1118 s,
1119 req,
1120 req->rex, req->wex,
1121 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001122 req->buf->i,
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001123 req->analysers);
1124
1125 if (s->flags & SN_ASSIGNED)
1126 goto no_cookie;
1127
Willy Tarreau37406352012-04-23 16:16:37 +02001128 memset(&smp, 0, sizeof(smp));
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001129
Willy Tarreau34db1082012-04-19 17:16:54 +02001130 args[0].type = ARGT_STR;
1131 args[0].data.str.str = s->be->rdp_cookie_name;
1132 args[0].data.str.len = s->be->rdp_cookie_len;
1133 args[1].type = ARGT_STOP;
1134
Willy Tarreauef38c392013-07-22 16:29:32 +02001135 ret = smp_fetch_rdp_cookie(px, s, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, args, &smp, NULL);
Willy Tarreauf853c462012-04-23 18:53:56 +02001136 if (ret == 0 || (smp.flags & SMP_F_MAY_CHANGE) || smp.data.str.len == 0)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001137 goto no_cookie;
1138
1139 memset(&addr, 0, sizeof(addr));
1140 addr.sin_family = AF_INET;
1141
Willy Tarreau664092c2011-12-16 19:11:42 +01001142 /* Considering an rdp cookie detected using acl, str ended with <cr><lf> and should return */
Willy Tarreauf853c462012-04-23 18:53:56 +02001143 addr.sin_addr.s_addr = strtoul(smp.data.str.str, &p, 10);
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001144 if (*p != '.')
1145 goto no_cookie;
1146 p++;
1147 addr.sin_port = (unsigned short)strtoul(p, &p, 10);
1148 if (*p != '.')
1149 goto no_cookie;
1150
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001151 s->target = NULL;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001152 while (srv) {
1153 if (memcmp(&addr, &(srv->addr), sizeof(addr)) == 0) {
1154 if ((srv->state & SRV_RUNNING) || (px->options & PR_O_PERSIST)) {
1155 /* we found the server and it is usable */
1156 s->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001157 s->target = &srv->obj_type;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001158 break;
1159 }
1160 }
1161 srv = srv->next;
1162 }
1163
1164no_cookie:
1165 req->analysers &= ~an_bit;
1166 req->analyse_exp = TICK_ETERNITY;
1167 return 1;
1168}
1169
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001170int be_downtime(struct proxy *px) {
Willy Tarreaub625a082007-11-26 01:15:43 +01001171 if (px->lbprm.tot_weight && px->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001172 return px->down_time;
1173
1174 return now.tv_sec - px->last_change + px->down_time;
1175}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001176
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001177/*
1178 * This function returns a string containing the balancing
1179 * mode of the proxy in a format suitable for stats.
1180 */
1181
1182const char *backend_lb_algo_str(int algo) {
1183
1184 if (algo == BE_LB_ALGO_RR)
1185 return "roundrobin";
1186 else if (algo == BE_LB_ALGO_SRR)
1187 return "static-rr";
Willy Tarreauf09c6602012-02-13 17:12:08 +01001188 else if (algo == BE_LB_ALGO_FAS)
1189 return "first";
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001190 else if (algo == BE_LB_ALGO_LC)
1191 return "leastconn";
1192 else if (algo == BE_LB_ALGO_SH)
1193 return "source";
1194 else if (algo == BE_LB_ALGO_UH)
1195 return "uri";
1196 else if (algo == BE_LB_ALGO_PH)
1197 return "url_param";
1198 else if (algo == BE_LB_ALGO_HH)
1199 return "hdr";
1200 else if (algo == BE_LB_ALGO_RCH)
1201 return "rdp-cookie";
1202 else
1203 return NULL;
1204}
1205
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001206/* This function parses a "balance" statement in a backend section describing
1207 * <curproxy>. It returns -1 if there is any error, otherwise zero. If it
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001208 * returns -1, it will write an error message into the <err> buffer which will
1209 * automatically be allocated and must be passed as NULL. The trailing '\n'
1210 * will not be written. The function must be called with <args> pointing to the
1211 * first word after "balance".
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001212 */
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001213int backend_parse_balance(const char **args, char **err, struct proxy *curproxy)
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001214{
1215 if (!*(args[0])) {
1216 /* if no option is set, use round-robin by default */
Willy Tarreau31682232007-11-29 15:38:04 +01001217 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1218 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001219 return 0;
1220 }
1221
1222 if (!strcmp(args[0], "roundrobin")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001223 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1224 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001225 }
Willy Tarreau9757a382009-10-03 12:56:50 +02001226 else if (!strcmp(args[0], "static-rr")) {
1227 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1228 curproxy->lbprm.algo |= BE_LB_ALGO_SRR;
1229 }
Willy Tarreauf09c6602012-02-13 17:12:08 +01001230 else if (!strcmp(args[0], "first")) {
1231 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1232 curproxy->lbprm.algo |= BE_LB_ALGO_FAS;
1233 }
Willy Tarreau51406232008-03-10 22:04:20 +01001234 else if (!strcmp(args[0], "leastconn")) {
1235 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1236 curproxy->lbprm.algo |= BE_LB_ALGO_LC;
1237 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001238 else if (!strcmp(args[0], "source")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001239 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1240 curproxy->lbprm.algo |= BE_LB_ALGO_SH;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001241 }
1242 else if (!strcmp(args[0], "uri")) {
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001243 int arg = 1;
1244
Willy Tarreau31682232007-11-29 15:38:04 +01001245 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1246 curproxy->lbprm.algo |= BE_LB_ALGO_UH;
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001247
Oskar Stolc8dc41842012-05-19 10:19:54 +01001248 curproxy->uri_whole = 0;
1249
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001250 while (*args[arg]) {
1251 if (!strcmp(args[arg], "len")) {
1252 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001253 memprintf(err, "%s : '%s' expects a positive integer (got '%s').", args[0], args[arg], args[arg+1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001254 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)) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001261 memprintf(err, "%s : '%s' expects a positive integer (got '%s').", args[0], args[arg], args[arg+1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001262 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 }
Oskar Stolc8dc41842012-05-19 10:19:54 +01001270 else if (!strcmp(args[arg], "whole")) {
1271 curproxy->uri_whole = 1;
1272 arg += 1;
1273 }
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001274 else {
Oskar Stolc8dc41842012-05-19 10:19:54 +01001275 memprintf(err, "%s only accepts parameters 'len', 'depth', and 'whole' (got '%s').", args[0], args[arg]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001276 return -1;
1277 }
1278 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001279 }
Willy Tarreau01732802007-11-01 22:48:15 +01001280 else if (!strcmp(args[0], "url_param")) {
1281 if (!*args[1]) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001282 memprintf(err, "%s requires an URL parameter name.", args[0]);
Willy Tarreau01732802007-11-01 22:48:15 +01001283 return -1;
1284 }
Willy Tarreau31682232007-11-29 15:38:04 +01001285 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1286 curproxy->lbprm.algo |= BE_LB_ALGO_PH;
Willy Tarreaua534fea2008-08-03 12:19:50 +02001287
1288 free(curproxy->url_param_name);
Willy Tarreau01732802007-11-01 22:48:15 +01001289 curproxy->url_param_name = strdup(args[1]);
Willy Tarreaua534fea2008-08-03 12:19:50 +02001290 curproxy->url_param_len = strlen(args[1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001291 if (*args[2]) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001292 if (strcmp(args[2], "check_post")) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001293 memprintf(err, "%s only accepts 'check_post' modifier (got '%s').", args[0], args[2]);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001294 return -1;
1295 }
1296 if (*args[3]) {
1297 /* TODO: maybe issue a warning if there is no value, no digits or too long */
1298 curproxy->url_param_post_limit = str2ui(args[3]);
1299 }
1300 /* if no limit, or faul value in args[3], then default to a moderate wordlen */
1301 if (!curproxy->url_param_post_limit)
1302 curproxy->url_param_post_limit = 48;
1303 else if ( curproxy->url_param_post_limit < 3 )
1304 curproxy->url_param_post_limit = 3; /* minimum example: S=3 or \r\nS=6& */
1305 }
Benoitaffb4812009-03-25 13:02:10 +01001306 }
1307 else if (!strncmp(args[0], "hdr(", 4)) {
1308 const char *beg, *end;
1309
1310 beg = args[0] + 4;
1311 end = strchr(beg, ')');
1312
1313 if (!end || end == beg) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001314 memprintf(err, "hdr requires an http header field name.");
Benoitaffb4812009-03-25 13:02:10 +01001315 return -1;
1316 }
1317
1318 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1319 curproxy->lbprm.algo |= BE_LB_ALGO_HH;
1320
1321 free(curproxy->hh_name);
1322 curproxy->hh_len = end - beg;
1323 curproxy->hh_name = my_strndup(beg, end - beg);
1324 curproxy->hh_match_domain = 0;
1325
1326 if (*args[1]) {
1327 if (strcmp(args[1], "use_domain_only")) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001328 memprintf(err, "%s only accepts 'use_domain_only' modifier (got '%s').", args[0], args[1]);
Benoitaffb4812009-03-25 13:02:10 +01001329 return -1;
1330 }
1331 curproxy->hh_match_domain = 1;
1332 }
1333
Emeric Brun736aa232009-06-30 17:56:00 +02001334 }
1335 else if (!strncmp(args[0], "rdp-cookie", 10)) {
1336 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1337 curproxy->lbprm.algo |= BE_LB_ALGO_RCH;
1338
1339 if ( *(args[0] + 10 ) == '(' ) { /* cookie name */
1340 const char *beg, *end;
1341
1342 beg = args[0] + 11;
1343 end = strchr(beg, ')');
1344
1345 if (!end || end == beg) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001346 memprintf(err, "rdp-cookie : missing cookie name.");
Emeric Brun736aa232009-06-30 17:56:00 +02001347 return -1;
1348 }
1349
1350 free(curproxy->hh_name);
1351 curproxy->hh_name = my_strndup(beg, end - beg);
1352 curproxy->hh_len = end - beg;
1353 }
1354 else if ( *(args[0] + 10 ) == '\0' ) { /* default cookie name 'mstshash' */
1355 free(curproxy->hh_name);
1356 curproxy->hh_name = strdup("mstshash");
1357 curproxy->hh_len = strlen(curproxy->hh_name);
1358 }
1359 else { /* syntax */
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001360 memprintf(err, "rdp-cookie : missing cookie name.");
Emeric Brun736aa232009-06-30 17:56:00 +02001361 return -1;
1362 }
Willy Tarreau01732802007-11-01 22:48:15 +01001363 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001364 else {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001365 memprintf(err, "only supports 'roundrobin', 'static-rr', 'leastconn', 'source', 'uri', 'url_param', 'hdr(name)' and 'rdp-cookie(name)' options.");
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001366 return -1;
1367 }
1368 return 0;
1369}
1370
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001371
1372/************************************************************************/
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001373/* All supported sample and ACL keywords must be declared here. */
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001374/************************************************************************/
1375
Willy Tarreau34db1082012-04-19 17:16:54 +02001376/* set temp integer to the number of enabled servers on the proxy.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001377 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001378 * undefined behaviour.
1379 */
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001380static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001381smp_fetch_nbsrv(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001382 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001383{
Willy Tarreau37406352012-04-23 16:16:37 +02001384 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001385 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001386 px = args->data.prx;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001387
1388 if (px->srv_act)
Willy Tarreauf853c462012-04-23 18:53:56 +02001389 smp->data.uint = px->srv_act;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001390 else if (px->lbprm.fbck)
Willy Tarreauf853c462012-04-23 18:53:56 +02001391 smp->data.uint = 1;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001392 else
Willy Tarreauf853c462012-04-23 18:53:56 +02001393 smp->data.uint = px->srv_bck;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001394
1395 return 1;
1396}
1397
Willy Tarreau37406352012-04-23 16:16:37 +02001398/* report in smp->flags a success or failure depending on the designated
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001399 * server's state. There is no match function involved since there's no pattern.
Willy Tarreau34db1082012-04-19 17:16:54 +02001400 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1401 * undefined behaviour.
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001402 */
1403static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001404smp_fetch_srv_is_up(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001405 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001406{
Willy Tarreau24e32d82012-04-23 23:55:44 +02001407 struct server *srv = args->data.srv;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001408
Willy Tarreau37406352012-04-23 16:16:37 +02001409 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001410 smp->type = SMP_T_BOOL;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001411 if (!(srv->state & SRV_MAINTAIN) &&
1412 (!(srv->state & SRV_CHECKED) || (srv->state & SRV_RUNNING)))
Willy Tarreau197e10a2012-04-23 19:18:42 +02001413 smp->data.uint = 1;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001414 else
Willy Tarreau197e10a2012-04-23 19:18:42 +02001415 smp->data.uint = 0;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001416 return 1;
1417}
1418
Willy Tarreau34db1082012-04-19 17:16:54 +02001419/* set temp integer to the number of enabled servers on the proxy.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001420 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001421 * undefined behaviour.
1422 */
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001423static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001424smp_fetch_connslots(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001425 const struct arg *args, struct sample *smp, const char *kw)
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001426{
1427 struct server *iterator;
Willy Tarreaud28c3532012-04-19 19:28:33 +02001428
Willy Tarreau37406352012-04-23 16:16:37 +02001429 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001430 smp->type = SMP_T_UINT;
1431 smp->data.uint = 0;
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001432
Willy Tarreau24e32d82012-04-23 23:55:44 +02001433 for (iterator = args->data.prx->srv; iterator; iterator = iterator->next) {
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001434 if ((iterator->state & SRV_RUNNING) == 0)
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001435 continue;
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001436
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001437 if (iterator->maxconn == 0 || iterator->maxqueue == 0) {
Willy Tarreaua5e37562011-12-16 17:06:15 +01001438 /* configuration is stupid */
Willy Tarreauf853c462012-04-23 18:53:56 +02001439 smp->data.uint = -1; /* FIXME: stupid value! */
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001440 return 1;
1441 }
1442
Willy Tarreauf853c462012-04-23 18:53:56 +02001443 smp->data.uint += (iterator->maxconn - iterator->cur_sess)
Willy Tarreau422aa072012-04-20 20:49:27 +02001444 + (iterator->maxqueue - iterator->nbpend);
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001445 }
1446
1447 return 1;
1448}
1449
Willy Tarreaua5e37562011-12-16 17:06:15 +01001450/* set temp integer to the id of the backend */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001451static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001452smp_fetch_be_id(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001453 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau37406352012-04-23 16:16:37 +02001454{
Willy Tarreauf853c462012-04-23 18:53:56 +02001455 smp->flags = SMP_F_VOL_TXN;
1456 smp->type = SMP_T_UINT;
1457 smp->data.uint = l4->be->uuid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001458 return 1;
1459}
1460
Willy Tarreaua5e37562011-12-16 17:06:15 +01001461/* set temp integer to the id of the server */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001462static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001463smp_fetch_srv_id(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001464 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau37406352012-04-23 16:16:37 +02001465{
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001466 if (!objt_server(l4->target))
Willy Tarreau17af4192011-02-23 14:27:06 +01001467 return 0;
1468
Willy Tarreauf853c462012-04-23 18:53:56 +02001469 smp->type = SMP_T_UINT;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001470 smp->data.uint = objt_server(l4->target)->puid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001471
1472 return 1;
1473}
1474
Willy Tarreau34db1082012-04-19 17:16:54 +02001475/* set temp integer to the number of connections per second reaching the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001476 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001477 * undefined behaviour.
1478 */
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001479static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001480smp_fetch_be_sess_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001481 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001482{
Willy Tarreau37406352012-04-23 16:16:37 +02001483 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001484 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001485 smp->data.uint = read_freq_ctr(&args->data.prx->be_sess_per_sec);
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001486 return 1;
1487}
1488
Willy Tarreau34db1082012-04-19 17:16:54 +02001489/* set temp integer to the number of concurrent connections on the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001490 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001491 * undefined behaviour.
1492 */
Willy Tarreaua36af912009-10-10 12:02:45 +02001493static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001494smp_fetch_be_conn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001495 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua36af912009-10-10 12:02:45 +02001496{
Willy Tarreau37406352012-04-23 16:16:37 +02001497 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001498 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001499 smp->data.uint = args->data.prx->beconn;
Willy Tarreaua36af912009-10-10 12:02:45 +02001500 return 1;
1501}
1502
Willy Tarreau34db1082012-04-19 17:16:54 +02001503/* set temp integer to the total number of queued connections on the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001504 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001505 * undefined behaviour.
1506 */
Willy Tarreaua36af912009-10-10 12:02:45 +02001507static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001508smp_fetch_queue_size(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001509 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua36af912009-10-10 12:02:45 +02001510{
Willy Tarreau37406352012-04-23 16:16:37 +02001511 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001512 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001513 smp->data.uint = args->data.prx->totpend;
Willy Tarreaua36af912009-10-10 12:02:45 +02001514 return 1;
1515}
1516
Willy Tarreaua5e37562011-12-16 17:06:15 +01001517/* set temp integer to the total number of queued connections on the backend divided
Willy Tarreaua36af912009-10-10 12:02:45 +02001518 * by the number of running servers and rounded up. If there is no running
1519 * server, we return twice the total, just as if we had half a running server.
1520 * This is more or less correct anyway, since we expect the last server to come
1521 * back soon.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001522 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001523 * undefined behaviour.
Willy Tarreaua36af912009-10-10 12:02:45 +02001524 */
1525static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001526smp_fetch_avg_queue_size(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001527 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua36af912009-10-10 12:02:45 +02001528{
1529 int nbsrv;
1530
Willy Tarreau37406352012-04-23 16:16:37 +02001531 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001532 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001533 px = args->data.prx;
Willy Tarreaua36af912009-10-10 12:02:45 +02001534
1535 if (px->srv_act)
1536 nbsrv = px->srv_act;
1537 else if (px->lbprm.fbck)
1538 nbsrv = 1;
1539 else
1540 nbsrv = px->srv_bck;
1541
1542 if (nbsrv > 0)
Willy Tarreauf853c462012-04-23 18:53:56 +02001543 smp->data.uint = (px->totpend + nbsrv - 1) / nbsrv;
Willy Tarreaua36af912009-10-10 12:02:45 +02001544 else
Willy Tarreauf853c462012-04-23 18:53:56 +02001545 smp->data.uint = px->totpend * 2;
Willy Tarreaua36af912009-10-10 12:02:45 +02001546
1547 return 1;
1548}
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001549
Willy Tarreau34db1082012-04-19 17:16:54 +02001550/* set temp integer to the number of concurrent connections on the server in the backend.
1551 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1552 * undefined behaviour.
1553 */
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001554static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001555smp_fetch_srv_conn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001556 const struct arg *args, struct sample *smp, const char *kw)
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001557{
Willy Tarreauf853c462012-04-23 18:53:56 +02001558 smp->flags = SMP_F_VOL_TEST;
1559 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001560 smp->data.uint = args->data.srv->cur_sess;
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001561 return 1;
1562}
1563
Tait Clarridge7896d522012-12-05 21:39:31 -05001564/* set temp integer to the number of enabled servers on the proxy.
1565 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1566 * undefined behaviour.
1567 */
1568static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001569smp_fetch_srv_sess_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001570 const struct arg *args, struct sample *smp, const char *kw)
Tait Clarridge7896d522012-12-05 21:39:31 -05001571{
1572 smp->flags = SMP_F_VOL_TEST;
1573 smp->type = SMP_T_UINT;
1574 smp->data.uint = read_freq_ctr(&args->data.srv->sess_per_sec);
1575 return 1;
1576}
1577
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001578
1579/* Note: must not be declared <const> as its list will be overwritten.
1580 * Please take care of keeping this list alphabetically sorted.
1581 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001582static struct sample_fetch_kw_list smp_kws = {ILH, {
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001583 { "avg_queue", smp_fetch_avg_queue_size, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1584 { "be_conn", smp_fetch_be_conn, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1585 { "be_id", smp_fetch_be_id, 0, NULL, SMP_T_UINT, SMP_USE_BKEND, },
1586 { "be_sess_rate", smp_fetch_be_sess_rate, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1587 { "connslots", smp_fetch_connslots, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1588 { "nbsrv", smp_fetch_nbsrv, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1589 { "queue", smp_fetch_queue_size, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1590 { "srv_conn", smp_fetch_srv_conn, ARG1(1,SRV), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1591 { "srv_id", smp_fetch_srv_id, 0, NULL, SMP_T_UINT, SMP_USE_SERVR, },
1592 { "srv_is_up", smp_fetch_srv_is_up, ARG1(1,SRV), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
1593 { "srv_sess_rate", smp_fetch_srv_sess_rate, ARG1(1,SRV), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1594 { /* END */ },
1595}};
1596
1597
Willy Tarreau61612d42012-04-19 18:42:05 +02001598/* Note: must not be declared <const> as its list will be overwritten.
1599 * Please take care of keeping this list alphabetically sorted.
1600 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001601static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001602 { /* END */ },
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001603}};
1604
1605
1606__attribute__((constructor))
1607static void __backend_init(void)
1608{
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001609 sample_register_fetches(&smp_kws);
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001610 acl_register_keywords(&acl_kws);
1611}
1612
Willy Tarreaubaaee002006-06-26 02:48:02 +02001613/*
1614 * Local variables:
1615 * c-indent-level: 8
1616 * c-basic-offset: 8
1617 * End:
1618 */