blob: 4bb2ea364d055077cd3ec98d56bd7f3ea2db3843 [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>
Bhaskar98634f02013-10-29 23:30:51 -040026#include <common/hash.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020027#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020029
Willy Tarreaubaaee002006-06-26 02:48:02 +020030#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020031
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +010032#include <proto/acl.h>
Willy Tarreau34db1082012-04-19 17:16:54 +020033#include <proto/arg.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020034#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020035#include <proto/channel.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020036#include <proto/frontend.h>
Willy Tarreau6b2e11b2009-10-01 07:52:15 +020037#include <proto/lb_chash.h>
Willy Tarreauf09c6602012-02-13 17:12:08 +010038#include <proto/lb_fas.h>
Willy Tarreauf89c1872009-10-01 11:19:37 +020039#include <proto/lb_fwlc.h>
40#include <proto/lb_fwrr.h>
41#include <proto/lb_map.h>
Willy Tarreau3fdb3662012-11-12 00:42:33 +010042#include <proto/obj_type.h>
Willy Tarreaud4c33c82013-01-07 21:59:07 +010043#include <proto/payload.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020044#include <proto/protocol.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020045#include <proto/proto_http.h>
Willy Tarreaua8f55d52010-05-31 17:44:19 +020046#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020047#include <proto/queue.h>
Willy Tarreaud4c33c82013-01-07 21:59:07 +010048#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010049#include <proto/server.h>
Willy Tarreau7c669d72008-06-20 15:04:11 +020050#include <proto/session.h>
Willy Tarreau75bf2c92012-08-20 17:01:35 +020051#include <proto/raw_sock.h>
Willy Tarreau9e000c62011-03-10 14:03:36 +010052#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/task.h>
54
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050055int be_lastsession(const struct proxy *be)
56{
57 if (be->be_counters.last_sess)
58 return now.tv_sec - be->be_counters.last_sess;
59
60 return -1;
61}
62
Bhaskar98634f02013-10-29 23:30:51 -040063/* helper function to invoke the correct hash method */
64static unsigned long gen_hash(const struct proxy* px, const char* key, unsigned long len)
65{
66 unsigned long hash;
67
68 switch (px->lbprm.algo & BE_LB_HASH_FUNC) {
69 case BE_LB_HFCN_DJB2:
70 hash = hash_djb2(key, len);
71 break;
Willy Tarreaua0f42712013-11-14 14:30:35 +010072 case BE_LB_HFCN_WT6:
73 hash = hash_wt6(key, len);
74 break;
Bhaskar98634f02013-10-29 23:30:51 -040075 case BE_LB_HFCN_SDBM:
76 /* this is the default hash function */
77 default:
78 hash = hash_sdbm(key, len);
79 break;
80 }
81
82 return hash;
83}
84
Willy Tarreaubaaee002006-06-26 02:48:02 +020085/*
86 * This function recounts the number of usable active and backup servers for
87 * proxy <p>. These numbers are returned into the p->srv_act and p->srv_bck.
Willy Tarreaub625a082007-11-26 01:15:43 +010088 * This function also recomputes the total active and backup weights. However,
Willy Tarreauf4cca452008-03-08 21:42:54 +010089 * it does not update tot_weight nor tot_used. Use update_backend_weight() for
Willy Tarreaub625a082007-11-26 01:15:43 +010090 * this.
Willy Tarreaubaaee002006-06-26 02:48:02 +020091 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +020092void recount_servers(struct proxy *px)
Willy Tarreaubaaee002006-06-26 02:48:02 +020093{
94 struct server *srv;
95
Willy Tarreau20697042007-11-15 23:26:18 +010096 px->srv_act = px->srv_bck = 0;
97 px->lbprm.tot_wact = px->lbprm.tot_wbck = 0;
Willy Tarreaub625a082007-11-26 01:15:43 +010098 px->lbprm.fbck = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +020099 for (srv = px->srv; srv != NULL; srv = srv->next) {
Willy Tarreaub625a082007-11-26 01:15:43 +0100100 if (!srv_is_usable(srv->state, srv->eweight))
101 continue;
102
103 if (srv->state & SRV_BACKUP) {
104 if (!px->srv_bck &&
Willy Tarreauf4cca452008-03-08 21:42:54 +0100105 !(px->options & PR_O_USE_ALL_BK))
Willy Tarreaub625a082007-11-26 01:15:43 +0100106 px->lbprm.fbck = srv;
107 px->srv_bck++;
108 px->lbprm.tot_wbck += srv->eweight;
109 } else {
110 px->srv_act++;
111 px->lbprm.tot_wact += srv->eweight;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200112 }
113 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100114}
Willy Tarreau20697042007-11-15 23:26:18 +0100115
Willy Tarreaub625a082007-11-26 01:15:43 +0100116/* This function simply updates the backend's tot_weight and tot_used values
117 * after servers weights have been updated. It is designed to be used after
118 * recount_servers() or equivalent.
119 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +0200120void update_backend_weight(struct proxy *px)
Willy Tarreaub625a082007-11-26 01:15:43 +0100121{
Willy Tarreau20697042007-11-15 23:26:18 +0100122 if (px->srv_act) {
123 px->lbprm.tot_weight = px->lbprm.tot_wact;
124 px->lbprm.tot_used = px->srv_act;
125 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100126 else if (px->lbprm.fbck) {
127 /* use only the first backup server */
128 px->lbprm.tot_weight = px->lbprm.fbck->eweight;
129 px->lbprm.tot_used = 1;
Willy Tarreau20697042007-11-15 23:26:18 +0100130 }
131 else {
Willy Tarreaub625a082007-11-26 01:15:43 +0100132 px->lbprm.tot_weight = px->lbprm.tot_wbck;
133 px->lbprm.tot_used = px->srv_bck;
Willy Tarreau20697042007-11-15 23:26:18 +0100134 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100135}
136
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200137/*
138 * This function tries to find a running server for the proxy <px> following
139 * the source hash method. Depending on the number of active/backup servers,
140 * it will either look for active servers, or for backup servers.
141 * If any server is found, it will be returned. If no valid server is found,
142 * NULL is returned.
143 */
144struct server *get_server_sh(struct proxy *px, const char *addr, int len)
145{
146 unsigned int h, l;
147
148 if (px->lbprm.tot_weight == 0)
149 return NULL;
150
151 l = h = 0;
152
153 /* note: we won't hash if there's only one server left */
154 if (px->lbprm.tot_used == 1)
155 goto hash_done;
156
157 while ((l + sizeof (int)) <= len) {
158 h ^= ntohl(*(unsigned int *)(&addr[l]));
159 l += sizeof (int);
160 }
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500161 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100162 h = full_hash(h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200163 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200164 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
165 return chash_get_server_hash(px, h);
166 else
167 return map_get_server_hash(px, h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200168}
169
170/*
171 * This function tries to find a running server for the proxy <px> following
172 * the URI hash method. In order to optimize cache hits, the hash computation
173 * ends at the question mark. Depending on the number of active/backup servers,
174 * it will either look for active servers, or for backup servers.
175 * If any server is found, it will be returned. If no valid server is found,
176 * NULL is returned.
177 *
178 * This code was contributed by Guillaume Dallaire, who also selected this hash
179 * algorithm out of a tens because it gave him the best results.
180 *
181 */
182struct server *get_server_uh(struct proxy *px, char *uri, int uri_len)
183{
184 unsigned long hash = 0;
185 int c;
186 int slashes = 0;
Bhaskar98634f02013-10-29 23:30:51 -0400187 const char *start, *end;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200188
189 if (px->lbprm.tot_weight == 0)
190 return NULL;
191
192 /* note: we won't hash if there's only one server left */
193 if (px->lbprm.tot_used == 1)
194 goto hash_done;
195
196 if (px->uri_len_limit)
197 uri_len = MIN(uri_len, px->uri_len_limit);
198
Bhaskar98634f02013-10-29 23:30:51 -0400199 start = end = uri;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200200 while (uri_len--) {
Bhaskar98634f02013-10-29 23:30:51 -0400201 c = *end++;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200202 if (c == '/') {
203 slashes++;
204 if (slashes == px->uri_dirs_depth1) /* depth+1 */
205 break;
206 }
Oskar Stolc8dc41842012-05-19 10:19:54 +0100207 else if (c == '?' && !px->uri_whole)
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200208 break;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200209 }
Bhaskar98634f02013-10-29 23:30:51 -0400210
211 hash = gen_hash(px, start, (end - start));
212
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500213 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100214 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200215 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200216 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
217 return chash_get_server_hash(px, hash);
218 else
219 return map_get_server_hash(px, hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200220}
221
Willy Tarreau01732802007-11-01 22:48:15 +0100222/*
223 * This function tries to find a running server for the proxy <px> following
224 * the URL parameter hash method. It looks for a specific parameter in the
225 * URL and hashes it to compute the server ID. This is useful to optimize
226 * performance by avoiding bounces between servers in contexts where sessions
227 * are shared but cookies are not usable. If the parameter is not found, NULL
228 * is returned. If any server is found, it will be returned. If no valid server
229 * is found, NULL is returned.
Willy Tarreau01732802007-11-01 22:48:15 +0100230 */
231struct server *get_server_ph(struct proxy *px, const char *uri, int uri_len)
232{
233 unsigned long hash = 0;
Bhaskar98634f02013-10-29 23:30:51 -0400234 const char *start, *end;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200235 const char *p;
236 const char *params;
Willy Tarreau01732802007-11-01 22:48:15 +0100237 int plen;
238
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200239 /* when tot_weight is 0 then so is srv_count */
Willy Tarreau20697042007-11-15 23:26:18 +0100240 if (px->lbprm.tot_weight == 0)
Willy Tarreau01732802007-11-01 22:48:15 +0100241 return NULL;
242
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200243 if ((p = memchr(uri, '?', uri_len)) == NULL)
244 return NULL;
245
Willy Tarreau01732802007-11-01 22:48:15 +0100246 p++;
247
248 uri_len -= (p - uri);
249 plen = px->url_param_len;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200250 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100251
252 while (uri_len > plen) {
253 /* Look for the parameter name followed by an equal symbol */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200254 if (params[plen] == '=') {
255 if (memcmp(params, px->url_param_name, plen) == 0) {
256 /* OK, we have the parameter here at <params>, and
Willy Tarreau01732802007-11-01 22:48:15 +0100257 * the value after the equal sign, at <p>
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200258 * skip the equal symbol
Willy Tarreau01732802007-11-01 22:48:15 +0100259 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200260 p += plen + 1;
Bhaskar98634f02013-10-29 23:30:51 -0400261 start = end = p;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200262 uri_len -= plen + 1;
263
Bhaskar98634f02013-10-29 23:30:51 -0400264 while (uri_len && *end != '&') {
Willy Tarreau01732802007-11-01 22:48:15 +0100265 uri_len--;
Bhaskar98634f02013-10-29 23:30:51 -0400266 end++;
Willy Tarreau01732802007-11-01 22:48:15 +0100267 }
Bhaskar98634f02013-10-29 23:30:51 -0400268 hash = gen_hash(px, start, (end - start));
269
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500270 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100271 hash = full_hash(hash);
Bhaskar98634f02013-10-29 23:30:51 -0400272
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200273 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
274 return chash_get_server_hash(px, hash);
275 else
276 return map_get_server_hash(px, hash);
Willy Tarreau01732802007-11-01 22:48:15 +0100277 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200278 }
279 /* skip to next parameter */
280 p = memchr(params, '&', uri_len);
281 if (!p)
282 return NULL;
283 p++;
284 uri_len -= (p - params);
285 params = p;
286 }
287 return NULL;
288}
289
290/*
291 * this does the same as the previous server_ph, but check the body contents
292 */
293struct server *get_server_ph_post(struct session *s)
294{
295 unsigned long hash = 0;
296 struct http_txn *txn = &s->txn;
Willy Tarreau7421efb2012-07-02 15:11:27 +0200297 struct channel *req = s->req;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200298 struct http_msg *msg = &txn->req;
299 struct proxy *px = s->be;
300 unsigned int plen = px->url_param_len;
Willy Tarreau2d8e4852014-04-17 20:08:17 +0200301 unsigned long len = http_body_bytes(msg);
Willy Tarreau0d090502014-04-17 20:31:44 +0200302 const char *params = b_ptr(req->buf, -http_data_rewind(msg));
Willy Tarreau157dd632009-12-06 19:18:09 +0100303 const char *p = params;
Bhaskar98634f02013-10-29 23:30:51 -0400304 const char *start, *end;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200305
Willy Tarreau157dd632009-12-06 19:18:09 +0100306 if (len == 0)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200307 return NULL;
308
Willy Tarreau157dd632009-12-06 19:18:09 +0100309 if (px->lbprm.tot_weight == 0)
310 return NULL;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200311
312 while (len > plen) {
313 /* Look for the parameter name followed by an equal symbol */
314 if (params[plen] == '=') {
315 if (memcmp(params, px->url_param_name, plen) == 0) {
316 /* OK, we have the parameter here at <params>, and
317 * the value after the equal sign, at <p>
318 * skip the equal symbol
319 */
320 p += plen + 1;
Bhaskar98634f02013-10-29 23:30:51 -0400321 start = end = p;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200322 len -= plen + 1;
323
Bhaskar98634f02013-10-29 23:30:51 -0400324 while (len && *end != '&') {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200325 if (unlikely(!HTTP_IS_TOKEN(*p))) {
Willy Tarreau157dd632009-12-06 19:18:09 +0100326 /* if in a POST, body must be URI encoded or it's not a URI.
Bhaskar98634f02013-10-29 23:30:51 -0400327 * Do not interpret any possible binary data as a parameter.
Willy Tarreau157dd632009-12-06 19:18:09 +0100328 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200329 if (likely(HTTP_IS_LWS(*p))) /* eol, uncertain uri len */
330 break;
331 return NULL; /* oh, no; this is not uri-encoded.
332 * This body does not contain parameters.
333 */
334 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200335 len--;
Bhaskar98634f02013-10-29 23:30:51 -0400336 end++;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200337 /* should we break if vlen exceeds limit? */
338 }
Bhaskar98634f02013-10-29 23:30:51 -0400339 hash = gen_hash(px, start, (end - start));
340
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500341 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100342 hash = full_hash(hash);
Bhaskar98634f02013-10-29 23:30:51 -0400343
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200344 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
345 return chash_get_server_hash(px, hash);
346 else
347 return map_get_server_hash(px, hash);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200348 }
349 }
Willy Tarreau01732802007-11-01 22:48:15 +0100350 /* skip to next parameter */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200351 p = memchr(params, '&', len);
Willy Tarreau01732802007-11-01 22:48:15 +0100352 if (!p)
353 return NULL;
354 p++;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200355 len -= (p - params);
356 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100357 }
358 return NULL;
359}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200360
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200361
Willy Tarreaubaaee002006-06-26 02:48:02 +0200362/*
Benoitaffb4812009-03-25 13:02:10 +0100363 * This function tries to find a running server for the proxy <px> following
364 * the Header parameter hash method. It looks for a specific parameter in the
365 * URL and hashes it to compute the server ID. This is useful to optimize
366 * performance by avoiding bounces between servers in contexts where sessions
367 * are shared but cookies are not usable. If the parameter is not found, NULL
368 * is returned. If any server is found, it will be returned. If no valid server
369 * is found, NULL is returned.
370 */
371struct server *get_server_hh(struct session *s)
372{
373 unsigned long hash = 0;
374 struct http_txn *txn = &s->txn;
Benoitaffb4812009-03-25 13:02:10 +0100375 struct proxy *px = s->be;
376 unsigned int plen = px->hh_len;
377 unsigned long len;
378 struct hdr_ctx ctx;
379 const char *p;
Bhaskar98634f02013-10-29 23:30:51 -0400380 const char *start, *end;
Benoitaffb4812009-03-25 13:02:10 +0100381
382 /* tot_weight appears to mean srv_count */
383 if (px->lbprm.tot_weight == 0)
384 return NULL;
385
Benoitaffb4812009-03-25 13:02:10 +0100386 ctx.idx = 0;
387
388 /* if the message is chunked, we skip the chunk size, but use the value as len */
Willy Tarreau211cdec2014-04-17 20:18:08 +0200389 http_find_header2(px->hh_name, plen, b_ptr(s->req->buf, -http_hdr_rewind(&txn->req)), &txn->hdr_idx, &ctx);
Benoitaffb4812009-03-25 13:02:10 +0100390
391 /* if the header is not found or empty, let's fallback to round robin */
392 if (!ctx.idx || !ctx.vlen)
393 return NULL;
394
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200395 /* note: we won't hash if there's only one server left */
396 if (px->lbprm.tot_used == 1)
397 goto hash_done;
398
Benoitaffb4812009-03-25 13:02:10 +0100399 /* Found a the hh_name in the headers.
400 * we will compute the hash based on this value ctx.val.
401 */
402 len = ctx.vlen;
403 p = (char *)ctx.line + ctx.val;
404 if (!px->hh_match_domain) {
Bhaskar98634f02013-10-29 23:30:51 -0400405 hash = gen_hash(px, p, len);
Benoitaffb4812009-03-25 13:02:10 +0100406 } else {
407 int dohash = 0;
408 p += len - 1;
Bhaskar98634f02013-10-29 23:30:51 -0400409 start = end = p;
Benoitaffb4812009-03-25 13:02:10 +0100410 /* special computation, use only main domain name, not tld/host
411 * going back from the end of string, start hashing at first
412 * dot stop at next.
413 * This is designed to work with the 'Host' header, and requires
414 * a special option to activate this.
415 */
416 while (len) {
417 if (*p == '.') {
Bhaskar98634f02013-10-29 23:30:51 -0400418 if (!dohash) {
Benoitaffb4812009-03-25 13:02:10 +0100419 dohash = 1;
Bhaskar98634f02013-10-29 23:30:51 -0400420 start = end = p - 1;
421 }
Benoitaffb4812009-03-25 13:02:10 +0100422 else
423 break;
424 } else {
425 if (dohash)
Bhaskar98634f02013-10-29 23:30:51 -0400426 start--;
Benoitaffb4812009-03-25 13:02:10 +0100427 }
428 len--;
429 p--;
430 }
Bhaskar98634f02013-10-29 23:30:51 -0400431 hash = gen_hash(px, start, (end - start));
Benoitaffb4812009-03-25 13:02:10 +0100432 }
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500433 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100434 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200435 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200436 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
437 return chash_get_server_hash(px, hash);
438 else
439 return map_get_server_hash(px, hash);
Benoitaffb4812009-03-25 13:02:10 +0100440}
441
Willy Tarreau34db1082012-04-19 17:16:54 +0200442/* RDP Cookie HASH. */
Emeric Brun736aa232009-06-30 17:56:00 +0200443struct server *get_server_rch(struct session *s)
444{
445 unsigned long hash = 0;
446 struct proxy *px = s->be;
447 unsigned long len;
Emeric Brun736aa232009-06-30 17:56:00 +0200448 int ret;
Willy Tarreau37406352012-04-23 16:16:37 +0200449 struct sample smp;
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200450 int rewind;
Emeric Brun736aa232009-06-30 17:56:00 +0200451
452 /* tot_weight appears to mean srv_count */
453 if (px->lbprm.tot_weight == 0)
454 return NULL;
455
Willy Tarreau37406352012-04-23 16:16:37 +0200456 memset(&smp, 0, sizeof(smp));
Emeric Brun736aa232009-06-30 17:56:00 +0200457
Willy Tarreau9b28e032012-10-12 23:49:43 +0200458 b_rew(s->req->buf, rewind = s->req->buf->o);
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200459
Willy Tarreaucadd8c92013-07-22 18:09:52 +0200460 ret = fetch_rdp_cookie_name(s, &smp, px->hh_name, px->hh_len);
Willy Tarreauf853c462012-04-23 18:53:56 +0200461 len = smp.data.str.len;
Willy Tarreau664092c2011-12-16 19:11:42 +0100462
Willy Tarreau9b28e032012-10-12 23:49:43 +0200463 b_adv(s->req->buf, rewind);
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200464
Willy Tarreau37406352012-04-23 16:16:37 +0200465 if (ret == 0 || (smp.flags & SMP_F_MAY_CHANGE) || len == 0)
Emeric Brun736aa232009-06-30 17:56:00 +0200466 return NULL;
467
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200468 /* note: we won't hash if there's only one server left */
469 if (px->lbprm.tot_used == 1)
470 goto hash_done;
471
Emeric Brun736aa232009-06-30 17:56:00 +0200472 /* Found a the hh_name in the headers.
473 * we will compute the hash based on this value ctx.val.
474 */
Bhaskar98634f02013-10-29 23:30:51 -0400475 hash = gen_hash(px, smp.data.str.str, len);
476
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500477 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100478 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200479 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200480 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
481 return chash_get_server_hash(px, hash);
482 else
483 return map_get_server_hash(px, hash);
Emeric Brun736aa232009-06-30 17:56:00 +0200484}
Benoitaffb4812009-03-25 13:02:10 +0100485
486/*
Willy Tarreau7c669d72008-06-20 15:04:11 +0200487 * This function applies the load-balancing algorithm to the session, as
488 * defined by the backend it is assigned to. The session is then marked as
489 * 'assigned'.
490 *
491 * This function MAY NOT be called with SN_ASSIGNED already set. If the session
492 * had a server previously assigned, it is rebalanced, trying to avoid the same
Willy Tarreau827aee92011-03-10 16:55:02 +0100493 * server, which should still be present in target_srv(&s->target) before the call.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200494 * The function tries to keep the original connection slot if it reconnects to
495 * the same server, otherwise it releases it and tries to offer it.
496 *
497 * It is illegal to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200498 *
499 * It may return :
Willy Tarreau664beb82011-03-10 11:38:29 +0100500 * SRV_STATUS_OK if everything is OK. ->srv and ->target are assigned.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200501 * SRV_STATUS_NOSRV if no server is available. Session is not ASSIGNED
502 * SRV_STATUS_FULL if all servers are saturated. Session is not ASSIGNED
Willy Tarreaubaaee002006-06-26 02:48:02 +0200503 * SRV_STATUS_INTERNAL for other unrecoverable errors.
504 *
Willy Tarreau7c669d72008-06-20 15:04:11 +0200505 * Upon successful return, the session flag SN_ASSIGNED is set to indicate that
Willy Tarreau827aee92011-03-10 16:55:02 +0100506 * it does not need to be called anymore. This means that target_srv(&s->target)
507 * can be trusted in balance and direct modes.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200508 *
509 */
510
511int assign_server(struct session *s)
512{
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200513 struct connection *conn;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200514 struct server *conn_slot;
Willy Tarreau827aee92011-03-10 16:55:02 +0100515 struct server *srv, *prev_srv;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200516 int err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100517
Simon Horman8effd3d2011-08-13 08:03:52 +0900518 DPRINTF(stderr,"assign_server : s=%p\n",s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200519
Willy Tarreau7c669d72008-06-20 15:04:11 +0200520 err = SRV_STATUS_INTERNAL;
521 if (unlikely(s->pend_pos || s->flags & SN_ASSIGNED))
522 goto out_err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100523
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100524 prev_srv = objt_server(s->target);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200525 conn_slot = s->srv_conn;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200526
Willy Tarreau7c669d72008-06-20 15:04:11 +0200527 /* We have to release any connection slot before applying any LB algo,
528 * otherwise we may erroneously end up with no available slot.
529 */
530 if (conn_slot)
531 sess_change_server(s, NULL);
532
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100533 /* We will now try to find the good server and store it into <objt_server(s->target)>.
534 * Note that <objt_server(s->target)> may be NULL in case of dispatch or proxy mode,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200535 * as well as if no server is available (check error code).
536 */
Willy Tarreau1a20a5d2007-11-01 21:08:19 +0100537
Willy Tarreau827aee92011-03-10 16:55:02 +0100538 srv = NULL;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100539 s->target = NULL;
Willy Tarreau9420b122013-12-15 18:58:25 +0100540 conn = objt_conn(s->req->cons->end);
Willy Tarreau664beb82011-03-10 11:38:29 +0100541
Willy Tarreau068621e2013-12-23 15:11:25 +0100542 if (conn &&
Willy Tarreau2481d162014-02-19 18:40:43 +0100543 (conn->flags & CO_FL_CONNECTED) &&
Willy Tarreau068621e2013-12-23 15:11:25 +0100544 ((s->be->options & PR_O_PREF_LAST) || (s->txn.flags & TX_PREFER_LAST)) &&
Willy Tarreau9420b122013-12-15 18:58:25 +0100545 objt_server(conn->target) && __objt_server(conn->target)->proxy == s->be &&
546 srv_is_usable(__objt_server(conn->target)->state, __objt_server(conn->target)->eweight)) {
547 /* This session was relying on a server in a previous request
548 * and the proxy has "option prefer-current-server" set, so
549 * let's try to reuse the same server.
550 */
551 srv = __objt_server(conn->target);
552 s->target = &srv->obj_type;
553 }
554 else if (s->be->lbprm.algo & BE_LB_KIND) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200555 /* we must check if we have at least one server available */
556 if (!s->be->lbprm.tot_weight) {
557 err = SRV_STATUS_NOSRV;
558 goto out;
559 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200560
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200561 /* First check whether we need to fetch some data or simply call
562 * the LB lookup function. Only the hashing functions will need
563 * some input data in fact, and will support multiple algorithms.
564 */
565 switch (s->be->lbprm.algo & BE_LB_LKUP) {
566 case BE_LB_LKUP_RRTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100567 srv = fwrr_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200568 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200569
Willy Tarreauf09c6602012-02-13 17:12:08 +0100570 case BE_LB_LKUP_FSTREE:
571 srv = fas_get_next_server(s->be, prev_srv);
572 break;
573
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200574 case BE_LB_LKUP_LCTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100575 srv = fwlc_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200576 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200577
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200578 case BE_LB_LKUP_CHTREE:
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200579 case BE_LB_LKUP_MAP:
Willy Tarreau9757a382009-10-03 12:56:50 +0200580 if ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200581 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100582 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200583 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100584 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau9757a382009-10-03 12:56:50 +0200585 break;
586 }
587 else if ((s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_HI) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200588 /* unknown balancing algorithm */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200589 err = SRV_STATUS_INTERNAL;
590 goto out;
591 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200592
593 switch (s->be->lbprm.algo & BE_LB_PARM) {
594 case BE_LB_HASH_SRC:
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200595 conn = objt_conn(s->req->prod->end);
596 if (conn && conn->addr.from.ss_family == AF_INET) {
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200597 srv = get_server_sh(s->be,
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200598 (void *)&((struct sockaddr_in *)&conn->addr.from)->sin_addr,
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200599 4);
600 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200601 else if (conn && conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200602 srv = get_server_sh(s->be,
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200603 (void *)&((struct sockaddr_in6 *)&conn->addr.from)->sin6_addr,
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200604 16);
605 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200606 else {
607 /* unknown IP family */
608 err = SRV_STATUS_INTERNAL;
609 goto out;
610 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200611 break;
612
613 case BE_LB_HASH_URI:
614 /* URI hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100615 if (s->txn.req.msg_state < HTTP_MSG_BODY)
616 break;
Willy Tarreau827aee92011-03-10 16:55:02 +0100617 srv = get_server_uh(s->be,
Willy Tarreauda6eed62014-04-17 20:24:24 +0200618 b_ptr(s->req->buf, -http_uri_rewind(&s->txn.req)),
Willy Tarreau827aee92011-03-10 16:55:02 +0100619 s->txn.req.sl.rq.u_l);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200620 break;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200621
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200622 case BE_LB_HASH_PRM:
623 /* URL Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100624 if (s->txn.req.msg_state < HTTP_MSG_BODY)
625 break;
Willy Tarreau61a21a32011-03-01 20:35:49 +0100626
Willy Tarreau827aee92011-03-10 16:55:02 +0100627 srv = get_server_ph(s->be,
Willy Tarreauda6eed62014-04-17 20:24:24 +0200628 b_ptr(s->req->buf, -http_uri_rewind(&s->txn.req)),
Willy Tarreau827aee92011-03-10 16:55:02 +0100629 s->txn.req.sl.rq.u_l);
Willy Tarreau61a21a32011-03-01 20:35:49 +0100630
Willy Tarreau827aee92011-03-10 16:55:02 +0100631 if (!srv && s->txn.meth == HTTP_METH_POST)
632 srv = get_server_ph_post(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200633 break;
Benoitaffb4812009-03-25 13:02:10 +0100634
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200635 case BE_LB_HASH_HDR:
636 /* Header Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100637 if (s->txn.req.msg_state < HTTP_MSG_BODY)
638 break;
Willy Tarreau827aee92011-03-10 16:55:02 +0100639 srv = get_server_hh(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200640 break;
641
642 case BE_LB_HASH_RDP:
643 /* RDP Cookie hashing */
Willy Tarreau827aee92011-03-10 16:55:02 +0100644 srv = get_server_rch(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200645 break;
646
647 default:
648 /* unknown balancing algorithm */
649 err = SRV_STATUS_INTERNAL;
650 goto out;
Benoitaffb4812009-03-25 13:02:10 +0100651 }
Emeric Brun736aa232009-06-30 17:56:00 +0200652
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200653 /* If the hashing parameter was not found, let's fall
654 * back to round robin on the map.
655 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100656 if (!srv) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200657 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100658 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200659 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100660 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200661 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200662
663 /* end of map-based LB */
Emeric Brun736aa232009-06-30 17:56:00 +0200664 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200665
Willy Tarreau7c669d72008-06-20 15:04:11 +0200666 default:
667 /* unknown balancing algorithm */
668 err = SRV_STATUS_INTERNAL;
669 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200670 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200671
Willy Tarreau827aee92011-03-10 16:55:02 +0100672 if (!srv) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200673 err = SRV_STATUS_FULL;
674 goto out;
675 }
Willy Tarreau827aee92011-03-10 16:55:02 +0100676 else if (srv != prev_srv) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100677 s->be->be_counters.cum_lbconn++;
Willy Tarreau827aee92011-03-10 16:55:02 +0100678 srv->counters.cum_lbconn++;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100679 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100680 s->target = &srv->obj_type;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200681 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200682 else if (s->be->options & (PR_O_DISPATCH | PR_O_TRANSP)) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100683 s->target = &s->be->obj_type;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200684 }
David du Colombier6f5ccb12011-03-10 22:26:24 +0100685 else if ((s->be->options & PR_O_HTTP_PROXY) &&
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200686 (conn = objt_conn(s->req->cons->end)) &&
687 is_addr(&conn->addr.to)) {
Willy Tarreau664beb82011-03-10 11:38:29 +0100688 /* in proxy mode, we need a valid destination address */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100689 s->target = &s->be->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +0100690 }
691 else {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200692 err = SRV_STATUS_NOSRV;
693 goto out;
694 }
695
696 s->flags |= SN_ASSIGNED;
697 err = SRV_STATUS_OK;
698 out:
699
700 /* Either we take back our connection slot, or we offer it to someone
701 * else if we don't need it anymore.
702 */
703 if (conn_slot) {
Willy Tarreau827aee92011-03-10 16:55:02 +0100704 if (conn_slot == srv) {
705 sess_change_server(s, srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200706 } else {
707 if (may_dequeue_tasks(conn_slot, s->be))
708 process_srv_queue(conn_slot);
709 }
710 }
711
712 out_err:
713 return err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200714}
715
716
717/*
718 * This function assigns a server address to a session, and sets SN_ADDR_SET.
719 * The address is taken from the currently assigned server, or from the
720 * dispatch or transparent address.
721 *
722 * It may return :
723 * SRV_STATUS_OK if everything is OK.
724 * SRV_STATUS_INTERNAL for other unrecoverable errors.
725 *
726 * Upon successful return, the session flag SN_ADDR_SET is set. This flag is
727 * not cleared, so it's to the caller to clear it if required.
728 *
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200729 * The caller is responsible for having already assigned a connection
730 * to si->end.
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200731 *
Willy Tarreaubaaee002006-06-26 02:48:02 +0200732 */
733int assign_server_address(struct session *s)
734{
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200735 struct connection *cli_conn = objt_conn(s->req->prod->end);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200736 struct connection *srv_conn = objt_conn(s->req->cons->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200737
Willy Tarreaubaaee002006-06-26 02:48:02 +0200738#ifdef DEBUG_FULL
739 fprintf(stderr,"assign_server_address : s=%p\n",s);
740#endif
741
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200742 if ((s->flags & SN_DIRECT) || (s->be->lbprm.algo & BE_LB_KIND)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200743 /* A server is necessarily known for this session */
744 if (!(s->flags & SN_ASSIGNED))
745 return SRV_STATUS_INTERNAL;
746
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200747 srv_conn->addr.to = objt_server(s->target)->addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200748
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200749 if (!is_addr(&srv_conn->addr.to) && cli_conn) {
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200750 /* if the server has no address, we use the same address
751 * the client asked, which is handy for remapping ports
752 * locally on multiple addresses at once.
753 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200754 conn_get_to_addr(cli_conn);
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200755
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200756 if (cli_conn->addr.to.ss_family == AF_INET) {
757 ((struct sockaddr_in *)&srv_conn->addr.to)->sin_addr = ((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
758 } else if (cli_conn->addr.to.ss_family == AF_INET6) {
759 ((struct sockaddr_in6 *)&srv_conn->addr.to)->sin6_addr = ((struct sockaddr_in6 *)&cli_conn->addr.to)->sin6_addr;
Emeric Brunec810d12010-10-22 16:36:33 +0200760 }
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200761 }
762
Willy Tarreaubaaee002006-06-26 02:48:02 +0200763 /* if this server remaps proxied ports, we'll use
764 * the port the client connected to with an offset. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200765 if ((objt_server(s->target)->state & SRV_MAPPORTS) && cli_conn) {
David du Colombier6f5ccb12011-03-10 22:26:24 +0100766 int base_port;
767
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200768 conn_get_to_addr(cli_conn);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100769
770 /* First, retrieve the port from the incoming connection */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200771 base_port = get_host_port(&cli_conn->addr.to);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100772
773 /* Second, assign the outgoing connection's port */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200774 base_port += get_host_port(&srv_conn->addr.to);
775 set_host_port(&srv_conn->addr.to, base_port);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200776 }
777 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200778 else if (s->be->options & PR_O_DISPATCH) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200779 /* connect to the defined dispatch addr */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200780 srv_conn->addr.to = s->be->dispatch_addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200781 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200782 else if ((s->be->options & PR_O_TRANSP) && cli_conn) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200783 /* in transparent mode, use the original dest addr if no dispatch specified */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200784 conn_get_to_addr(cli_conn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200785
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200786 if (cli_conn->addr.to.ss_family == AF_INET || cli_conn->addr.to.ss_family == AF_INET6)
787 srv_conn->addr.to = cli_conn->addr.to;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200788 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100789 else if (s->be->options & PR_O_HTTP_PROXY) {
790 /* If HTTP PROXY option is set, then server is already assigned
791 * during incoming client request parsing. */
792 }
Willy Tarreau1a1158b2007-01-20 11:07:46 +0100793 else {
794 /* no server and no LB algorithm ! */
795 return SRV_STATUS_INTERNAL;
796 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200797
798 s->flags |= SN_ADDR_SET;
799 return SRV_STATUS_OK;
800}
801
802
803/* This function assigns a server to session <s> if required, and can add the
804 * connection to either the assigned server's queue or to the proxy's queue.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200805 * If ->srv_conn is set, the session is first released from the server.
806 * It may also be called with SN_DIRECT and/or SN_ASSIGNED though. It will
807 * be called before any connection and after any retry or redispatch occurs.
808 *
809 * It is not allowed to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200810 *
811 * Returns :
812 *
813 * SRV_STATUS_OK if everything is OK.
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100814 * SRV_STATUS_NOSRV if no server is available. objt_server(s->target) = NULL.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200815 * SRV_STATUS_QUEUED if the connection has been queued.
816 * SRV_STATUS_FULL if the server(s) is/are saturated and the
Willy Tarreau827aee92011-03-10 16:55:02 +0100817 * connection could not be queued at the server's,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200818 * which may be NULL if we queue on the backend.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200819 * SRV_STATUS_INTERNAL for other unrecoverable errors.
820 *
821 */
822int assign_server_and_queue(struct session *s)
823{
824 struct pendconn *p;
Willy Tarreau827aee92011-03-10 16:55:02 +0100825 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200826 int err;
827
828 if (s->pend_pos)
829 return SRV_STATUS_INTERNAL;
830
Willy Tarreau7c669d72008-06-20 15:04:11 +0200831 err = SRV_STATUS_OK;
832 if (!(s->flags & SN_ASSIGNED)) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100833 struct server *prev_srv = objt_server(s->target);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100834
Willy Tarreau7c669d72008-06-20 15:04:11 +0200835 err = assign_server(s);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100836 if (prev_srv) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200837 /* This session was previously assigned to a server. We have to
838 * update the session's and the server's stats :
839 * - if the server changed :
840 * - set TX_CK_DOWN if txn.flags was TX_CK_VALID
841 * - set SN_REDISP if it was successfully redispatched
842 * - increment srv->redispatches and be->redispatches
843 * - if the server remained the same : update retries.
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100844 */
845
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100846 if (prev_srv != objt_server(s->target)) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200847 if ((s->txn.flags & TX_CK_MASK) == TX_CK_VALID) {
848 s->txn.flags &= ~TX_CK_MASK;
849 s->txn.flags |= TX_CK_DOWN;
850 }
851 s->flags |= SN_REDISP;
Willy Tarreau3d80d912011-03-10 11:42:13 +0100852 prev_srv->counters.redispatches++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100853 s->be->be_counters.redispatches++;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200854 } else {
Willy Tarreau3d80d912011-03-10 11:42:13 +0100855 prev_srv->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100856 s->be->be_counters.retries++;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100857 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100858 }
859 }
860
Willy Tarreaubaaee002006-06-26 02:48:02 +0200861 switch (err) {
862 case SRV_STATUS_OK:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200863 /* we have SN_ASSIGNED set */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100864 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100865 if (!srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200866 return SRV_STATUS_OK; /* dispatch or proxy mode */
867
868 /* If we already have a connection slot, no need to check any queue */
Willy Tarreau827aee92011-03-10 16:55:02 +0100869 if (s->srv_conn == srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200870 return SRV_STATUS_OK;
871
872 /* OK, this session already has an assigned server, but no
873 * connection slot yet. Either it is a redispatch, or it was
874 * assigned from persistence information (direct mode).
875 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100876 if ((s->flags & SN_REDIRECTABLE) && srv->rdr_len) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200877 /* server scheduled for redirection, and already assigned. We
878 * don't want to go further nor check the queue.
Willy Tarreau21d2af32008-02-14 20:25:24 +0100879 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100880 sess_change_server(s, srv); /* not really needed in fact */
Willy Tarreau21d2af32008-02-14 20:25:24 +0100881 return SRV_STATUS_OK;
882 }
883
Willy Tarreau7c669d72008-06-20 15:04:11 +0200884 /* We might have to queue this session if the assigned server is full.
885 * We know we have to queue it into the server's queue, so if a maxqueue
886 * is set on the server, we must also check that the server's queue is
887 * not full, in which case we have to return FULL.
888 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100889 if (srv->maxconn &&
890 (srv->nbpend || srv->served >= srv_dynamic_maxconn(srv))) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200891
Willy Tarreau827aee92011-03-10 16:55:02 +0100892 if (srv->maxqueue > 0 && srv->nbpend >= srv->maxqueue)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200893 return SRV_STATUS_FULL;
894
Willy Tarreaubaaee002006-06-26 02:48:02 +0200895 p = pendconn_add(s);
896 if (p)
897 return SRV_STATUS_QUEUED;
898 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200899 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200900 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200901
902 /* OK, we can use this server. Let's reserve our place */
Willy Tarreau827aee92011-03-10 16:55:02 +0100903 sess_change_server(s, srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200904 return SRV_STATUS_OK;
905
906 case SRV_STATUS_FULL:
907 /* queue this session into the proxy's queue */
908 p = pendconn_add(s);
909 if (p)
910 return SRV_STATUS_QUEUED;
911 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200912 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200913
914 case SRV_STATUS_NOSRV:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200915 return err;
916
Willy Tarreaubaaee002006-06-26 02:48:02 +0200917 case SRV_STATUS_INTERNAL:
918 return err;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200919
Willy Tarreaubaaee002006-06-26 02:48:02 +0200920 default:
921 return SRV_STATUS_INTERNAL;
922 }
Willy Tarreau5b6995c2008-01-13 16:31:17 +0100923}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200924
Willy Tarreaub1d67742010-03-29 19:36:59 +0200925/* If an explicit source binding is specified on the server and/or backend, and
926 * this source makes use of the transparent proxy, then it is extracted now and
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200927 * assigned to the session's pending connection. This function assumes that an
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200928 * outgoing connection has already been assigned to s->req->cons->end.
Willy Tarreaub1d67742010-03-29 19:36:59 +0200929 */
930static void assign_tproxy_address(struct session *s)
931{
Pieter Baauwd551fb52013-05-08 22:49:23 +0200932#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_TRANSPARENT)
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100933 struct server *srv = objt_server(s->target);
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100934 struct conn_src *src;
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200935 struct connection *cli_conn;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200936 struct connection *srv_conn = objt_conn(s->req->cons->end);
Willy Tarreau827aee92011-03-10 16:55:02 +0100937
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100938 if (srv && srv->conn_src.opts & CO_SRC_BIND)
939 src = &srv->conn_src;
940 else if (s->be->conn_src.opts & CO_SRC_BIND)
941 src = &s->be->conn_src;
942 else
943 return;
Willy Tarreau294c4732011-12-16 21:35:50 +0100944
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100945 switch (src->opts & CO_SRC_TPROXY_MASK) {
946 case CO_SRC_TPROXY_ADDR:
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200947 srv_conn->addr.from = src->tproxy_addr;
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100948 break;
949 case CO_SRC_TPROXY_CLI:
950 case CO_SRC_TPROXY_CIP:
951 /* FIXME: what can we do if the client connects in IPv6 or unix socket ? */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200952 cli_conn = objt_conn(s->req->prod->end);
953 if (cli_conn)
954 srv_conn->addr.from = cli_conn->addr.from;
955 else
956 memset(&srv_conn->addr.from, 0, sizeof(srv_conn->addr.from));
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100957 break;
958 case CO_SRC_TPROXY_DYN:
959 if (src->bind_hdr_occ) {
960 char *vptr;
961 int vlen;
962 int rewind;
Willy Tarreau294c4732011-12-16 21:35:50 +0100963
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100964 /* bind to the IP in a header */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200965 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_family = AF_INET;
966 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_port = 0;
967 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_addr.s_addr = 0;
Willy Tarreau294c4732011-12-16 21:35:50 +0100968
Willy Tarreau211cdec2014-04-17 20:18:08 +0200969 b_rew(s->req->buf, rewind = http_hdr_rewind(&s->txn.req));
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100970 if (http_get_hdr(&s->txn.req, src->bind_hdr_name, src->bind_hdr_len,
971 &s->txn.hdr_idx, src->bind_hdr_occ, NULL, &vptr, &vlen)) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200972 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_addr.s_addr =
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100973 htonl(inetaddr_host_lim(vptr, vptr + vlen));
Willy Tarreaubce70882009-09-07 11:51:47 +0200974 }
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100975 b_adv(s->req->buf, rewind);
Willy Tarreaub1d67742010-03-29 19:36:59 +0200976 }
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100977 break;
978 default:
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200979 memset(&srv_conn->addr.from, 0, sizeof(srv_conn->addr.from));
Willy Tarreaub1d67742010-03-29 19:36:59 +0200980 }
981#endif
982}
983
984
Willy Tarreaubaaee002006-06-26 02:48:02 +0200985/*
986 * This function initiates a connection to the server assigned to this session
Willy Tarreau6471afb2011-09-23 10:54:59 +0200987 * (s->target, s->req->cons->addr.to). It will assign a server if none
Willy Tarreau664beb82011-03-10 11:38:29 +0100988 * is assigned yet.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200989 * It can return one of :
990 * - SN_ERR_NONE if everything's OK
991 * - SN_ERR_SRVTO if there are no more servers
992 * - SN_ERR_SRVCL if the connection was refused by the server
993 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
994 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
995 * - SN_ERR_INTERNAL for any other purely internal errors
996 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200997 * The server-facing stream interface is expected to hold a pre-allocated connection
998 * in s->req->cons->conn.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200999 */
1000int connect_server(struct session *s)
1001{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001002 struct connection *cli_conn;
Willy Tarreau34601a82013-12-15 16:33:46 +01001003 struct connection *srv_conn;
Willy Tarreau827aee92011-03-10 16:55:02 +01001004 struct server *srv;
Willy Tarreau34601a82013-12-15 16:33:46 +01001005 int reuse = 0;
Willy Tarreau9650f372009-08-16 14:02:45 +02001006 int err;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001007
Willy Tarreau34601a82013-12-15 16:33:46 +01001008 srv_conn = objt_conn(s->req->cons->end);
1009 if (srv_conn)
1010 reuse = s->target == srv_conn->target;
1011
1012 if (reuse) {
1013 /* Disable connection reuse if a dynamic source is used.
1014 * As long as we don't share connections between servers,
1015 * we don't need to disable connection reuse on no-idempotent
1016 * requests nor when PROXY protocol is used.
1017 */
1018 srv = objt_server(s->target);
1019 if (srv && srv->conn_src.opts & CO_SRC_BIND) {
1020 if ((srv->conn_src.opts & CO_SRC_TPROXY_MASK) == CO_SRC_TPROXY_DYN)
1021 reuse = 0;
1022 }
1023 else if (s->be->conn_src.opts & CO_SRC_BIND) {
1024 if ((s->be->conn_src.opts & CO_SRC_TPROXY_MASK) == CO_SRC_TPROXY_DYN)
1025 reuse = 0;
1026 }
1027 }
1028
1029 srv_conn = si_alloc_conn(s->req->cons, reuse);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001030 if (!srv_conn)
1031 return SN_ERR_RESOURCE;
1032
Willy Tarreaubaaee002006-06-26 02:48:02 +02001033 if (!(s->flags & SN_ADDR_SET)) {
1034 err = assign_server_address(s);
1035 if (err != SRV_STATUS_OK)
1036 return SN_ERR_INTERNAL;
1037 }
1038
Willy Tarreauff605db2013-12-20 11:09:51 +01001039 if (!conn_xprt_ready(srv_conn)) {
1040 /* the target was only on the session, assign it to the SI now */
1041 srv_conn->target = s->target;
Willy Tarreaud88edf22009-06-14 15:48:17 +02001042
Willy Tarreauff605db2013-12-20 11:09:51 +01001043 /* set the correct protocol on the output stream interface */
1044 if (objt_server(s->target)) {
1045 conn_prepare(srv_conn, objt_server(s->target)->proto, objt_server(s->target)->xprt);
1046 }
1047 else if (obj_type(s->target) == OBJ_TYPE_PROXY) {
1048 /* proxies exclusively run on raw_sock right now */
1049 conn_prepare(srv_conn, protocol_by_family(srv_conn->addr.to.ss_family), &raw_sock);
1050 if (!objt_conn(s->req->cons->end) || !objt_conn(s->req->cons->end)->ctrl)
1051 return SN_ERR_INTERNAL;
1052 }
1053 else
1054 return SN_ERR_INTERNAL; /* how did we get there ? */
Willy Tarreaud02394b2012-05-11 18:32:18 +02001055
Willy Tarreauff605db2013-12-20 11:09:51 +01001056 /* process the case where the server requires the PROXY protocol to be sent */
1057 srv_conn->send_proxy_ofs = 0;
1058 if (objt_server(s->target) && (objt_server(s->target)->state & SRV_SEND_PROXY)) {
1059 srv_conn->send_proxy_ofs = 1; /* must compute size */
1060 cli_conn = objt_conn(s->req->prod->end);
1061 if (cli_conn)
1062 conn_get_to_addr(cli_conn);
1063 }
Willy Tarreau2a6e8802013-10-24 15:50:53 +02001064
Willy Tarreauff605db2013-12-20 11:09:51 +01001065 si_attach_conn(s->req->cons, srv_conn);
Willy Tarreau26d8c592012-05-07 18:12:14 +02001066
Willy Tarreauff605db2013-12-20 11:09:51 +01001067 assign_tproxy_address(s);
1068 }
1069 else {
1070 /* the connection is being reused, just re-attach it */
1071 si_attach_conn(s->req->cons, srv_conn);
Willy Tarreau36346242014-02-24 18:26:30 +01001072 s->flags |= SN_SRV_REUSED;
Willy Tarreauff605db2013-12-20 11:09:51 +01001073 }
Willy Tarreaub1d67742010-03-29 19:36:59 +02001074
William Lallemandb7ff6a32012-03-02 14:35:21 +01001075 /* flag for logging source ip/port */
1076 if (s->fe->options2 & PR_O2_SRC_ADDR)
1077 s->req->cons->flags |= SI_FL_SRC_ADDR;
1078
Willy Tarreau14f8e862012-08-30 22:23:13 +02001079 /* disable lingering */
1080 if (s->be->options & PR_O_TCP_NOLING)
1081 s->req->cons->flags |= SI_FL_NOLINGER;
1082
Willy Tarreau73b013b2012-05-21 16:31:45 +02001083 err = si_connect(s->req->cons);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001084
Willy Tarreau9650f372009-08-16 14:02:45 +02001085 if (err != SN_ERR_NONE)
1086 return err;
Willy Tarreau788e2842008-08-26 13:25:39 +02001087
Willy Tarreau14f8e862012-08-30 22:23:13 +02001088 /* set connect timeout */
1089 s->req->cons->exp = tick_add_ifset(now_ms, s->be->timeout.connect);
1090
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001091 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001092 if (srv) {
Willy Tarreau1e62de62008-11-11 20:20:02 +01001093 s->flags |= SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001094 srv->cur_sess++;
1095 if (srv->cur_sess > srv->counters.cur_sess_max)
1096 srv->counters.cur_sess_max = srv->cur_sess;
Willy Tarreau51406232008-03-10 22:04:20 +01001097 if (s->be->lbprm.server_take_conn)
Willy Tarreau827aee92011-03-10 16:55:02 +01001098 s->be->lbprm.server_take_conn(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001099 }
1100
Willy Tarreaubaaee002006-06-26 02:48:02 +02001101 return SN_ERR_NONE; /* connection is OK */
1102}
1103
1104
Willy Tarreaubaaee002006-06-26 02:48:02 +02001105/* This function performs the "redispatch" part of a connection attempt. It
1106 * will assign a server if required, queue the connection if required, and
1107 * handle errors that might arise at this level. It can change the server
1108 * state. It will return 1 if it encounters an error, switches the server
1109 * state, or has to queue a connection. Otherwise, it will return 0 indicating
1110 * that the connection is ready to use.
1111 */
1112
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001113int srv_redispatch_connect(struct session *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001114{
Willy Tarreau827aee92011-03-10 16:55:02 +01001115 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001116 int conn_err;
1117
1118 /* We know that we don't have any connection pending, so we will
1119 * try to get a new one, and wait in this state if it's queued
1120 */
Willy Tarreau7c669d72008-06-20 15:04:11 +02001121 redispatch:
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001122 conn_err = assign_server_and_queue(s);
1123 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001124
Willy Tarreaubaaee002006-06-26 02:48:02 +02001125 switch (conn_err) {
1126 case SRV_STATUS_OK:
1127 break;
1128
Willy Tarreau7c669d72008-06-20 15:04:11 +02001129 case SRV_STATUS_FULL:
1130 /* The server has reached its maxqueue limit. Either PR_O_REDISP is set
1131 * and we can redispatch to another server, or it is not and we return
1132 * 503. This only makes sense in DIRECT mode however, because normal LB
1133 * algorithms would never select such a server, and hash algorithms
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001134 * would bring us on the same server again. Note that s->target is set
Willy Tarreau827aee92011-03-10 16:55:02 +01001135 * in this case.
Willy Tarreau7c669d72008-06-20 15:04:11 +02001136 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001137 if (((s->flags & (SN_DIRECT|SN_FORCE_PRST)) == SN_DIRECT) &&
1138 (s->be->options & PR_O_REDISP)) {
1139 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Willy Tarreau7c669d72008-06-20 15:04:11 +02001140 goto redispatch;
1141 }
1142
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001143 if (!s->req->cons->err_type) {
1144 s->req->cons->err_type = SI_ET_QUEUE_ERR;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001145 }
Willy Tarreau7c669d72008-06-20 15:04:11 +02001146
Willy Tarreau827aee92011-03-10 16:55:02 +01001147 srv->counters.failed_conns++;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001148 s->be->be_counters.failed_conns++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02001149 return 1;
1150
Willy Tarreaubaaee002006-06-26 02:48:02 +02001151 case SRV_STATUS_NOSRV:
Willy Tarreau827aee92011-03-10 16:55:02 +01001152 /* note: it is guaranteed that srv == NULL here */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001153 if (!s->req->cons->err_type) {
1154 s->req->cons->err_type = SI_ET_CONN_ERR;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001155 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +01001156
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001157 s->be->be_counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001158 return 1;
1159
1160 case SRV_STATUS_QUEUED:
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001161 s->req->cons->exp = tick_add_ifset(now_ms, s->be->timeout.queue);
1162 s->req->cons->state = SI_ST_QUE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001163 /* do nothing else and do not wake any other session up */
1164 return 1;
1165
Willy Tarreaubaaee002006-06-26 02:48:02 +02001166 case SRV_STATUS_INTERNAL:
1167 default:
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001168 if (!s->req->cons->err_type) {
1169 s->req->cons->err_type = SI_ET_CONN_OTHER;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001170 }
1171
Willy Tarreau827aee92011-03-10 16:55:02 +01001172 if (srv)
1173 srv_inc_sess_ctr(srv);
1174 if (srv)
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001175 srv_set_sess_last(srv);
1176 if (srv)
Willy Tarreau827aee92011-03-10 16:55:02 +01001177 srv->counters.failed_conns++;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001178 s->be->be_counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001179
1180 /* release other sessions waiting for this server */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001181 if (may_dequeue_tasks(srv, s->be))
Willy Tarreau827aee92011-03-10 16:55:02 +01001182 process_srv_queue(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001183 return 1;
1184 }
1185 /* if we get here, it's because we got SRV_STATUS_OK, which also
1186 * means that the connection has not been queued.
1187 */
1188 return 0;
1189}
1190
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001191/* Apply RDP cookie persistence to the current session. For this, the function
1192 * tries to extract an RDP cookie from the request buffer, and look for the
1193 * matching server in the list. If the server is found, it is assigned to the
1194 * session. This always returns 1, and the analyser removes itself from the
1195 * list. Nothing is performed if a server was already assigned.
1196 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02001197int tcp_persist_rdp_cookie(struct session *s, struct channel *req, int an_bit)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001198{
1199 struct proxy *px = s->be;
1200 int ret;
Willy Tarreau37406352012-04-23 16:16:37 +02001201 struct sample smp;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001202 struct server *srv = px->srv;
1203 struct sockaddr_in addr;
1204 char *p;
1205
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001206 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 +02001207 now_ms, __FUNCTION__,
1208 s,
1209 req,
1210 req->rex, req->wex,
1211 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001212 req->buf->i,
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001213 req->analysers);
1214
1215 if (s->flags & SN_ASSIGNED)
1216 goto no_cookie;
1217
Willy Tarreau37406352012-04-23 16:16:37 +02001218 memset(&smp, 0, sizeof(smp));
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001219
Willy Tarreaucadd8c92013-07-22 18:09:52 +02001220 ret = fetch_rdp_cookie_name(s, &smp, s->be->rdp_cookie_name, s->be->rdp_cookie_len);
Willy Tarreauf853c462012-04-23 18:53:56 +02001221 if (ret == 0 || (smp.flags & SMP_F_MAY_CHANGE) || smp.data.str.len == 0)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001222 goto no_cookie;
1223
1224 memset(&addr, 0, sizeof(addr));
1225 addr.sin_family = AF_INET;
1226
Willy Tarreau664092c2011-12-16 19:11:42 +01001227 /* Considering an rdp cookie detected using acl, str ended with <cr><lf> and should return */
Willy Tarreauf853c462012-04-23 18:53:56 +02001228 addr.sin_addr.s_addr = strtoul(smp.data.str.str, &p, 10);
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001229 if (*p != '.')
1230 goto no_cookie;
1231 p++;
1232 addr.sin_port = (unsigned short)strtoul(p, &p, 10);
1233 if (*p != '.')
1234 goto no_cookie;
1235
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001236 s->target = NULL;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001237 while (srv) {
1238 if (memcmp(&addr, &(srv->addr), sizeof(addr)) == 0) {
1239 if ((srv->state & SRV_RUNNING) || (px->options & PR_O_PERSIST)) {
1240 /* we found the server and it is usable */
1241 s->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001242 s->target = &srv->obj_type;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001243 break;
1244 }
1245 }
1246 srv = srv->next;
1247 }
1248
1249no_cookie:
1250 req->analysers &= ~an_bit;
1251 req->analyse_exp = TICK_ETERNITY;
1252 return 1;
1253}
1254
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001255int be_downtime(struct proxy *px) {
Willy Tarreaub625a082007-11-26 01:15:43 +01001256 if (px->lbprm.tot_weight && px->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001257 return px->down_time;
1258
1259 return now.tv_sec - px->last_change + px->down_time;
1260}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001261
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001262/*
1263 * This function returns a string containing the balancing
1264 * mode of the proxy in a format suitable for stats.
1265 */
1266
1267const char *backend_lb_algo_str(int algo) {
1268
1269 if (algo == BE_LB_ALGO_RR)
1270 return "roundrobin";
1271 else if (algo == BE_LB_ALGO_SRR)
1272 return "static-rr";
Willy Tarreauf09c6602012-02-13 17:12:08 +01001273 else if (algo == BE_LB_ALGO_FAS)
1274 return "first";
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001275 else if (algo == BE_LB_ALGO_LC)
1276 return "leastconn";
1277 else if (algo == BE_LB_ALGO_SH)
1278 return "source";
1279 else if (algo == BE_LB_ALGO_UH)
1280 return "uri";
1281 else if (algo == BE_LB_ALGO_PH)
1282 return "url_param";
1283 else if (algo == BE_LB_ALGO_HH)
1284 return "hdr";
1285 else if (algo == BE_LB_ALGO_RCH)
1286 return "rdp-cookie";
1287 else
1288 return NULL;
1289}
1290
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001291/* This function parses a "balance" statement in a backend section describing
1292 * <curproxy>. It returns -1 if there is any error, otherwise zero. If it
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001293 * returns -1, it will write an error message into the <err> buffer which will
1294 * automatically be allocated and must be passed as NULL. The trailing '\n'
1295 * will not be written. The function must be called with <args> pointing to the
1296 * first word after "balance".
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001297 */
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001298int backend_parse_balance(const char **args, char **err, struct proxy *curproxy)
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001299{
1300 if (!*(args[0])) {
1301 /* if no option is set, use round-robin by default */
Willy Tarreau31682232007-11-29 15:38:04 +01001302 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1303 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001304 return 0;
1305 }
1306
1307 if (!strcmp(args[0], "roundrobin")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001308 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1309 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001310 }
Willy Tarreau9757a382009-10-03 12:56:50 +02001311 else if (!strcmp(args[0], "static-rr")) {
1312 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1313 curproxy->lbprm.algo |= BE_LB_ALGO_SRR;
1314 }
Willy Tarreauf09c6602012-02-13 17:12:08 +01001315 else if (!strcmp(args[0], "first")) {
1316 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1317 curproxy->lbprm.algo |= BE_LB_ALGO_FAS;
1318 }
Willy Tarreau51406232008-03-10 22:04:20 +01001319 else if (!strcmp(args[0], "leastconn")) {
1320 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1321 curproxy->lbprm.algo |= BE_LB_ALGO_LC;
1322 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001323 else if (!strcmp(args[0], "source")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001324 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1325 curproxy->lbprm.algo |= BE_LB_ALGO_SH;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001326 }
1327 else if (!strcmp(args[0], "uri")) {
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001328 int arg = 1;
1329
Willy Tarreau31682232007-11-29 15:38:04 +01001330 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1331 curproxy->lbprm.algo |= BE_LB_ALGO_UH;
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001332
Oskar Stolc8dc41842012-05-19 10:19:54 +01001333 curproxy->uri_whole = 0;
1334
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001335 while (*args[arg]) {
1336 if (!strcmp(args[arg], "len")) {
1337 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001338 memprintf(err, "%s : '%s' expects a positive integer (got '%s').", args[0], args[arg], args[arg+1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001339 return -1;
1340 }
1341 curproxy->uri_len_limit = atoi(args[arg+1]);
1342 arg += 2;
1343 }
1344 else if (!strcmp(args[arg], "depth")) {
1345 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001346 memprintf(err, "%s : '%s' expects a positive integer (got '%s').", args[0], args[arg], args[arg+1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001347 return -1;
1348 }
1349 /* hint: we store the position of the ending '/' (depth+1) so
1350 * that we avoid a comparison while computing the hash.
1351 */
1352 curproxy->uri_dirs_depth1 = atoi(args[arg+1]) + 1;
1353 arg += 2;
1354 }
Oskar Stolc8dc41842012-05-19 10:19:54 +01001355 else if (!strcmp(args[arg], "whole")) {
1356 curproxy->uri_whole = 1;
1357 arg += 1;
1358 }
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001359 else {
Oskar Stolc8dc41842012-05-19 10:19:54 +01001360 memprintf(err, "%s only accepts parameters 'len', 'depth', and 'whole' (got '%s').", args[0], args[arg]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001361 return -1;
1362 }
1363 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001364 }
Willy Tarreau01732802007-11-01 22:48:15 +01001365 else if (!strcmp(args[0], "url_param")) {
1366 if (!*args[1]) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001367 memprintf(err, "%s requires an URL parameter name.", args[0]);
Willy Tarreau01732802007-11-01 22:48:15 +01001368 return -1;
1369 }
Willy Tarreau31682232007-11-29 15:38:04 +01001370 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1371 curproxy->lbprm.algo |= BE_LB_ALGO_PH;
Willy Tarreaua534fea2008-08-03 12:19:50 +02001372
1373 free(curproxy->url_param_name);
Willy Tarreau01732802007-11-01 22:48:15 +01001374 curproxy->url_param_name = strdup(args[1]);
Willy Tarreaua534fea2008-08-03 12:19:50 +02001375 curproxy->url_param_len = strlen(args[1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001376 if (*args[2]) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001377 if (strcmp(args[2], "check_post")) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001378 memprintf(err, "%s only accepts 'check_post' modifier (got '%s').", args[0], args[2]);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001379 return -1;
1380 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001381 }
Benoitaffb4812009-03-25 13:02:10 +01001382 }
1383 else if (!strncmp(args[0], "hdr(", 4)) {
1384 const char *beg, *end;
1385
1386 beg = args[0] + 4;
1387 end = strchr(beg, ')');
1388
1389 if (!end || end == beg) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001390 memprintf(err, "hdr requires an http header field name.");
Benoitaffb4812009-03-25 13:02:10 +01001391 return -1;
1392 }
1393
1394 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1395 curproxy->lbprm.algo |= BE_LB_ALGO_HH;
1396
1397 free(curproxy->hh_name);
1398 curproxy->hh_len = end - beg;
1399 curproxy->hh_name = my_strndup(beg, end - beg);
1400 curproxy->hh_match_domain = 0;
1401
1402 if (*args[1]) {
1403 if (strcmp(args[1], "use_domain_only")) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001404 memprintf(err, "%s only accepts 'use_domain_only' modifier (got '%s').", args[0], args[1]);
Benoitaffb4812009-03-25 13:02:10 +01001405 return -1;
1406 }
1407 curproxy->hh_match_domain = 1;
1408 }
Emeric Brun736aa232009-06-30 17:56:00 +02001409 }
1410 else if (!strncmp(args[0], "rdp-cookie", 10)) {
1411 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1412 curproxy->lbprm.algo |= BE_LB_ALGO_RCH;
1413
1414 if ( *(args[0] + 10 ) == '(' ) { /* cookie name */
1415 const char *beg, *end;
1416
1417 beg = args[0] + 11;
1418 end = strchr(beg, ')');
1419
1420 if (!end || end == beg) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001421 memprintf(err, "rdp-cookie : missing cookie name.");
Emeric Brun736aa232009-06-30 17:56:00 +02001422 return -1;
1423 }
1424
1425 free(curproxy->hh_name);
1426 curproxy->hh_name = my_strndup(beg, end - beg);
1427 curproxy->hh_len = end - beg;
1428 }
1429 else if ( *(args[0] + 10 ) == '\0' ) { /* default cookie name 'mstshash' */
1430 free(curproxy->hh_name);
1431 curproxy->hh_name = strdup("mstshash");
1432 curproxy->hh_len = strlen(curproxy->hh_name);
1433 }
1434 else { /* syntax */
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001435 memprintf(err, "rdp-cookie : missing cookie name.");
Emeric Brun736aa232009-06-30 17:56:00 +02001436 return -1;
1437 }
Willy Tarreau01732802007-11-01 22:48:15 +01001438 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001439 else {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001440 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 +01001441 return -1;
1442 }
1443 return 0;
1444}
1445
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001446
1447/************************************************************************/
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001448/* All supported sample and ACL keywords must be declared here. */
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001449/************************************************************************/
1450
Willy Tarreau34db1082012-04-19 17:16:54 +02001451/* set temp integer to the number of enabled servers on the proxy.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001452 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001453 * undefined behaviour.
1454 */
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001455static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001456smp_fetch_nbsrv(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001457 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001458{
Willy Tarreau37406352012-04-23 16:16:37 +02001459 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001460 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001461 px = args->data.prx;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001462
1463 if (px->srv_act)
Willy Tarreauf853c462012-04-23 18:53:56 +02001464 smp->data.uint = px->srv_act;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001465 else if (px->lbprm.fbck)
Willy Tarreauf853c462012-04-23 18:53:56 +02001466 smp->data.uint = 1;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001467 else
Willy Tarreauf853c462012-04-23 18:53:56 +02001468 smp->data.uint = px->srv_bck;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001469
1470 return 1;
1471}
1472
Willy Tarreau37406352012-04-23 16:16:37 +02001473/* report in smp->flags a success or failure depending on the designated
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001474 * server's state. There is no match function involved since there's no pattern.
Willy Tarreau34db1082012-04-19 17:16:54 +02001475 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1476 * undefined behaviour.
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001477 */
1478static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001479smp_fetch_srv_is_up(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001480 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001481{
Willy Tarreau24e32d82012-04-23 23:55:44 +02001482 struct server *srv = args->data.srv;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001483
Willy Tarreau37406352012-04-23 16:16:37 +02001484 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001485 smp->type = SMP_T_BOOL;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001486 if (!(srv->state & SRV_MAINTAIN) &&
Willy Tarreauff5ae352013-12-11 20:36:34 +01001487 (!(srv->check.state & CHK_ST_CONFIGURED) || (srv->state & SRV_RUNNING)))
Willy Tarreau197e10a2012-04-23 19:18:42 +02001488 smp->data.uint = 1;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001489 else
Willy Tarreau197e10a2012-04-23 19:18:42 +02001490 smp->data.uint = 0;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001491 return 1;
1492}
1493
Willy Tarreau34db1082012-04-19 17:16:54 +02001494/* set temp integer to the number of enabled servers on the proxy.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001495 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001496 * undefined behaviour.
1497 */
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001498static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001499smp_fetch_connslots(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001500 const struct arg *args, struct sample *smp, const char *kw)
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001501{
1502 struct server *iterator;
Willy Tarreaud28c3532012-04-19 19:28:33 +02001503
Willy Tarreau37406352012-04-23 16:16:37 +02001504 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001505 smp->type = SMP_T_UINT;
1506 smp->data.uint = 0;
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001507
Willy Tarreau24e32d82012-04-23 23:55:44 +02001508 for (iterator = args->data.prx->srv; iterator; iterator = iterator->next) {
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001509 if ((iterator->state & SRV_RUNNING) == 0)
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001510 continue;
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001511
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001512 if (iterator->maxconn == 0 || iterator->maxqueue == 0) {
Willy Tarreaua5e37562011-12-16 17:06:15 +01001513 /* configuration is stupid */
Willy Tarreauf853c462012-04-23 18:53:56 +02001514 smp->data.uint = -1; /* FIXME: stupid value! */
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001515 return 1;
1516 }
1517
Willy Tarreauf853c462012-04-23 18:53:56 +02001518 smp->data.uint += (iterator->maxconn - iterator->cur_sess)
Willy Tarreau422aa072012-04-20 20:49:27 +02001519 + (iterator->maxqueue - iterator->nbpend);
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001520 }
1521
1522 return 1;
1523}
1524
Willy Tarreaua5e37562011-12-16 17:06:15 +01001525/* set temp integer to the id of the backend */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001526static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001527smp_fetch_be_id(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001528 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau37406352012-04-23 16:16:37 +02001529{
Willy Tarreauf853c462012-04-23 18:53:56 +02001530 smp->flags = SMP_F_VOL_TXN;
1531 smp->type = SMP_T_UINT;
1532 smp->data.uint = l4->be->uuid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001533 return 1;
1534}
1535
Willy Tarreaua5e37562011-12-16 17:06:15 +01001536/* set temp integer to the id of the server */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001537static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001538smp_fetch_srv_id(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001539 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau37406352012-04-23 16:16:37 +02001540{
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001541 if (!objt_server(l4->target))
Willy Tarreau17af4192011-02-23 14:27:06 +01001542 return 0;
1543
Willy Tarreauf853c462012-04-23 18:53:56 +02001544 smp->type = SMP_T_UINT;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001545 smp->data.uint = objt_server(l4->target)->puid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001546
1547 return 1;
1548}
1549
Willy Tarreau34db1082012-04-19 17:16:54 +02001550/* set temp integer to the number of connections per second reaching the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001551 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001552 * undefined behaviour.
1553 */
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001554static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001555smp_fetch_be_sess_rate(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)
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001557{
Willy Tarreau37406352012-04-23 16:16:37 +02001558 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001559 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001560 smp->data.uint = read_freq_ctr(&args->data.prx->be_sess_per_sec);
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001561 return 1;
1562}
1563
Willy Tarreau34db1082012-04-19 17:16:54 +02001564/* set temp integer to the number of concurrent connections on the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001565 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001566 * undefined behaviour.
1567 */
Willy Tarreaua36af912009-10-10 12:02:45 +02001568static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001569smp_fetch_be_conn(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)
Willy Tarreaua36af912009-10-10 12:02:45 +02001571{
Willy Tarreau37406352012-04-23 16:16:37 +02001572 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001573 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001574 smp->data.uint = args->data.prx->beconn;
Willy Tarreaua36af912009-10-10 12:02:45 +02001575 return 1;
1576}
1577
Willy Tarreau34db1082012-04-19 17:16:54 +02001578/* set temp integer to the total number of queued connections on the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001579 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001580 * undefined behaviour.
1581 */
Willy Tarreaua36af912009-10-10 12:02:45 +02001582static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001583smp_fetch_queue_size(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001584 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua36af912009-10-10 12:02:45 +02001585{
Willy Tarreau37406352012-04-23 16:16:37 +02001586 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001587 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001588 smp->data.uint = args->data.prx->totpend;
Willy Tarreaua36af912009-10-10 12:02:45 +02001589 return 1;
1590}
1591
Willy Tarreaua5e37562011-12-16 17:06:15 +01001592/* set temp integer to the total number of queued connections on the backend divided
Willy Tarreaua36af912009-10-10 12:02:45 +02001593 * by the number of running servers and rounded up. If there is no running
1594 * server, we return twice the total, just as if we had half a running server.
1595 * This is more or less correct anyway, since we expect the last server to come
1596 * back soon.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001597 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001598 * undefined behaviour.
Willy Tarreaua36af912009-10-10 12:02:45 +02001599 */
1600static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001601smp_fetch_avg_queue_size(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001602 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua36af912009-10-10 12:02:45 +02001603{
1604 int nbsrv;
1605
Willy Tarreau37406352012-04-23 16:16:37 +02001606 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001607 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001608 px = args->data.prx;
Willy Tarreaua36af912009-10-10 12:02:45 +02001609
1610 if (px->srv_act)
1611 nbsrv = px->srv_act;
1612 else if (px->lbprm.fbck)
1613 nbsrv = 1;
1614 else
1615 nbsrv = px->srv_bck;
1616
1617 if (nbsrv > 0)
Willy Tarreauf853c462012-04-23 18:53:56 +02001618 smp->data.uint = (px->totpend + nbsrv - 1) / nbsrv;
Willy Tarreaua36af912009-10-10 12:02:45 +02001619 else
Willy Tarreauf853c462012-04-23 18:53:56 +02001620 smp->data.uint = px->totpend * 2;
Willy Tarreaua36af912009-10-10 12:02:45 +02001621
1622 return 1;
1623}
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001624
Willy Tarreau34db1082012-04-19 17:16:54 +02001625/* set temp integer to the number of concurrent connections on the server in the backend.
1626 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1627 * undefined behaviour.
1628 */
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001629static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001630smp_fetch_srv_conn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001631 const struct arg *args, struct sample *smp, const char *kw)
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001632{
Willy Tarreauf853c462012-04-23 18:53:56 +02001633 smp->flags = SMP_F_VOL_TEST;
1634 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001635 smp->data.uint = args->data.srv->cur_sess;
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001636 return 1;
1637}
1638
Tait Clarridge7896d522012-12-05 21:39:31 -05001639/* set temp integer to the number of enabled servers on the proxy.
1640 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1641 * undefined behaviour.
1642 */
1643static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001644smp_fetch_srv_sess_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001645 const struct arg *args, struct sample *smp, const char *kw)
Tait Clarridge7896d522012-12-05 21:39:31 -05001646{
1647 smp->flags = SMP_F_VOL_TEST;
1648 smp->type = SMP_T_UINT;
1649 smp->data.uint = read_freq_ctr(&args->data.srv->sess_per_sec);
1650 return 1;
1651}
1652
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001653
1654/* Note: must not be declared <const> as its list will be overwritten.
1655 * Please take care of keeping this list alphabetically sorted.
1656 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001657static struct sample_fetch_kw_list smp_kws = {ILH, {
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001658 { "avg_queue", smp_fetch_avg_queue_size, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1659 { "be_conn", smp_fetch_be_conn, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1660 { "be_id", smp_fetch_be_id, 0, NULL, SMP_T_UINT, SMP_USE_BKEND, },
1661 { "be_sess_rate", smp_fetch_be_sess_rate, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1662 { "connslots", smp_fetch_connslots, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1663 { "nbsrv", smp_fetch_nbsrv, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1664 { "queue", smp_fetch_queue_size, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1665 { "srv_conn", smp_fetch_srv_conn, ARG1(1,SRV), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1666 { "srv_id", smp_fetch_srv_id, 0, NULL, SMP_T_UINT, SMP_USE_SERVR, },
1667 { "srv_is_up", smp_fetch_srv_is_up, ARG1(1,SRV), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
1668 { "srv_sess_rate", smp_fetch_srv_sess_rate, ARG1(1,SRV), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1669 { /* END */ },
1670}};
1671
1672
Willy Tarreau61612d42012-04-19 18:42:05 +02001673/* Note: must not be declared <const> as its list will be overwritten.
1674 * Please take care of keeping this list alphabetically sorted.
1675 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001676static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001677 { /* END */ },
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001678}};
1679
1680
1681__attribute__((constructor))
1682static void __backend_init(void)
1683{
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001684 sample_register_fetches(&smp_kws);
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001685 acl_register_keywords(&acl_kws);
1686}
1687
Willy Tarreaubaaee002006-06-26 02:48:02 +02001688/*
1689 * Local variables:
1690 * c-indent-level: 8
1691 * c-basic-offset: 8
1692 * End:
1693 */