blob: 8fcce7dd2ecae48d858903e4022f60ee297f27e8 [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
Bhaskar98634f02013-10-29 23:30:51 -040055/* helper function to invoke the correct hash method */
56static unsigned long gen_hash(const struct proxy* px, const char* key, unsigned long len)
57{
58 unsigned long hash;
59
60 switch (px->lbprm.algo & BE_LB_HASH_FUNC) {
61 case BE_LB_HFCN_DJB2:
62 hash = hash_djb2(key, len);
63 break;
Willy Tarreaua0f42712013-11-14 14:30:35 +010064 case BE_LB_HFCN_WT6:
65 hash = hash_wt6(key, len);
66 break;
Bhaskar98634f02013-10-29 23:30:51 -040067 case BE_LB_HFCN_SDBM:
68 /* this is the default hash function */
69 default:
70 hash = hash_sdbm(key, len);
71 break;
72 }
73
74 return hash;
75}
76
Willy Tarreaubaaee002006-06-26 02:48:02 +020077/*
78 * This function recounts the number of usable active and backup servers for
79 * proxy <p>. These numbers are returned into the p->srv_act and p->srv_bck.
Willy Tarreaub625a082007-11-26 01:15:43 +010080 * This function also recomputes the total active and backup weights. However,
Willy Tarreauf4cca452008-03-08 21:42:54 +010081 * it does not update tot_weight nor tot_used. Use update_backend_weight() for
Willy Tarreaub625a082007-11-26 01:15:43 +010082 * this.
Willy Tarreaubaaee002006-06-26 02:48:02 +020083 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +020084void recount_servers(struct proxy *px)
Willy Tarreaubaaee002006-06-26 02:48:02 +020085{
86 struct server *srv;
87
Willy Tarreau20697042007-11-15 23:26:18 +010088 px->srv_act = px->srv_bck = 0;
89 px->lbprm.tot_wact = px->lbprm.tot_wbck = 0;
Willy Tarreaub625a082007-11-26 01:15:43 +010090 px->lbprm.fbck = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +020091 for (srv = px->srv; srv != NULL; srv = srv->next) {
Willy Tarreaub625a082007-11-26 01:15:43 +010092 if (!srv_is_usable(srv->state, srv->eweight))
93 continue;
94
95 if (srv->state & SRV_BACKUP) {
96 if (!px->srv_bck &&
Willy Tarreauf4cca452008-03-08 21:42:54 +010097 !(px->options & PR_O_USE_ALL_BK))
Willy Tarreaub625a082007-11-26 01:15:43 +010098 px->lbprm.fbck = srv;
99 px->srv_bck++;
100 px->lbprm.tot_wbck += srv->eweight;
101 } else {
102 px->srv_act++;
103 px->lbprm.tot_wact += srv->eweight;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200104 }
105 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100106}
Willy Tarreau20697042007-11-15 23:26:18 +0100107
Willy Tarreaub625a082007-11-26 01:15:43 +0100108/* This function simply updates the backend's tot_weight and tot_used values
109 * after servers weights have been updated. It is designed to be used after
110 * recount_servers() or equivalent.
111 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +0200112void update_backend_weight(struct proxy *px)
Willy Tarreaub625a082007-11-26 01:15:43 +0100113{
Willy Tarreau20697042007-11-15 23:26:18 +0100114 if (px->srv_act) {
115 px->lbprm.tot_weight = px->lbprm.tot_wact;
116 px->lbprm.tot_used = px->srv_act;
117 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100118 else if (px->lbprm.fbck) {
119 /* use only the first backup server */
120 px->lbprm.tot_weight = px->lbprm.fbck->eweight;
121 px->lbprm.tot_used = 1;
Willy Tarreau20697042007-11-15 23:26:18 +0100122 }
123 else {
Willy Tarreaub625a082007-11-26 01:15:43 +0100124 px->lbprm.tot_weight = px->lbprm.tot_wbck;
125 px->lbprm.tot_used = px->srv_bck;
Willy Tarreau20697042007-11-15 23:26:18 +0100126 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100127}
128
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200129/*
130 * This function tries to find a running server for the proxy <px> following
131 * the source hash method. Depending on the number of active/backup servers,
132 * it will either look for active servers, or for backup servers.
133 * If any server is found, it will be returned. If no valid server is found,
134 * NULL is returned.
135 */
136struct server *get_server_sh(struct proxy *px, const char *addr, int len)
137{
138 unsigned int h, l;
139
140 if (px->lbprm.tot_weight == 0)
141 return NULL;
142
143 l = h = 0;
144
145 /* note: we won't hash if there's only one server left */
146 if (px->lbprm.tot_used == 1)
147 goto hash_done;
148
149 while ((l + sizeof (int)) <= len) {
150 h ^= ntohl(*(unsigned int *)(&addr[l]));
151 l += sizeof (int);
152 }
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500153 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100154 h = full_hash(h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200155 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200156 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
157 return chash_get_server_hash(px, h);
158 else
159 return map_get_server_hash(px, h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200160}
161
162/*
163 * This function tries to find a running server for the proxy <px> following
164 * the URI hash method. In order to optimize cache hits, the hash computation
165 * ends at the question mark. Depending on the number of active/backup servers,
166 * it will either look for active servers, or for backup servers.
167 * If any server is found, it will be returned. If no valid server is found,
168 * NULL is returned.
169 *
170 * This code was contributed by Guillaume Dallaire, who also selected this hash
171 * algorithm out of a tens because it gave him the best results.
172 *
173 */
174struct server *get_server_uh(struct proxy *px, char *uri, int uri_len)
175{
176 unsigned long hash = 0;
177 int c;
178 int slashes = 0;
Bhaskar98634f02013-10-29 23:30:51 -0400179 const char *start, *end;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200180
181 if (px->lbprm.tot_weight == 0)
182 return NULL;
183
184 /* note: we won't hash if there's only one server left */
185 if (px->lbprm.tot_used == 1)
186 goto hash_done;
187
188 if (px->uri_len_limit)
189 uri_len = MIN(uri_len, px->uri_len_limit);
190
Bhaskar98634f02013-10-29 23:30:51 -0400191 start = end = uri;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200192 while (uri_len--) {
Bhaskar98634f02013-10-29 23:30:51 -0400193 c = *end++;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200194 if (c == '/') {
195 slashes++;
196 if (slashes == px->uri_dirs_depth1) /* depth+1 */
197 break;
198 }
Oskar Stolc8dc41842012-05-19 10:19:54 +0100199 else if (c == '?' && !px->uri_whole)
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200200 break;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200201 }
Bhaskar98634f02013-10-29 23:30:51 -0400202
203 hash = gen_hash(px, start, (end - start));
204
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500205 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100206 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200207 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200208 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
209 return chash_get_server_hash(px, hash);
210 else
211 return map_get_server_hash(px, hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200212}
213
Willy Tarreau01732802007-11-01 22:48:15 +0100214/*
215 * This function tries to find a running server for the proxy <px> following
216 * the URL parameter hash method. It looks for a specific parameter in the
217 * URL and hashes it to compute the server ID. This is useful to optimize
218 * performance by avoiding bounces between servers in contexts where sessions
219 * are shared but cookies are not usable. If the parameter is not found, NULL
220 * is returned. If any server is found, it will be returned. If no valid server
221 * is found, NULL is returned.
Willy Tarreau01732802007-11-01 22:48:15 +0100222 */
223struct server *get_server_ph(struct proxy *px, const char *uri, int uri_len)
224{
225 unsigned long hash = 0;
Bhaskar98634f02013-10-29 23:30:51 -0400226 const char *start, *end;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200227 const char *p;
228 const char *params;
Willy Tarreau01732802007-11-01 22:48:15 +0100229 int plen;
230
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200231 /* when tot_weight is 0 then so is srv_count */
Willy Tarreau20697042007-11-15 23:26:18 +0100232 if (px->lbprm.tot_weight == 0)
Willy Tarreau01732802007-11-01 22:48:15 +0100233 return NULL;
234
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200235 if ((p = memchr(uri, '?', uri_len)) == NULL)
236 return NULL;
237
Willy Tarreau01732802007-11-01 22:48:15 +0100238 p++;
239
240 uri_len -= (p - uri);
241 plen = px->url_param_len;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200242 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100243
244 while (uri_len > plen) {
245 /* Look for the parameter name followed by an equal symbol */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200246 if (params[plen] == '=') {
247 if (memcmp(params, px->url_param_name, plen) == 0) {
248 /* OK, we have the parameter here at <params>, and
Willy Tarreau01732802007-11-01 22:48:15 +0100249 * the value after the equal sign, at <p>
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200250 * skip the equal symbol
Willy Tarreau01732802007-11-01 22:48:15 +0100251 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200252 p += plen + 1;
Bhaskar98634f02013-10-29 23:30:51 -0400253 start = end = p;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200254 uri_len -= plen + 1;
255
Bhaskar98634f02013-10-29 23:30:51 -0400256 while (uri_len && *end != '&') {
Willy Tarreau01732802007-11-01 22:48:15 +0100257 uri_len--;
Bhaskar98634f02013-10-29 23:30:51 -0400258 end++;
Willy Tarreau01732802007-11-01 22:48:15 +0100259 }
Bhaskar98634f02013-10-29 23:30:51 -0400260 hash = gen_hash(px, start, (end - start));
261
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500262 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100263 hash = full_hash(hash);
Bhaskar98634f02013-10-29 23:30:51 -0400264
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200265 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
266 return chash_get_server_hash(px, hash);
267 else
268 return map_get_server_hash(px, hash);
Willy Tarreau01732802007-11-01 22:48:15 +0100269 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200270 }
271 /* skip to next parameter */
272 p = memchr(params, '&', uri_len);
273 if (!p)
274 return NULL;
275 p++;
276 uri_len -= (p - params);
277 params = p;
278 }
279 return NULL;
280}
281
282/*
283 * this does the same as the previous server_ph, but check the body contents
284 */
285struct server *get_server_ph_post(struct session *s)
286{
287 unsigned long hash = 0;
288 struct http_txn *txn = &s->txn;
Willy Tarreau7421efb2012-07-02 15:11:27 +0200289 struct channel *req = s->req;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200290 struct http_msg *msg = &txn->req;
291 struct proxy *px = s->be;
292 unsigned int plen = px->url_param_len;
Willy Tarreau124d9912011-03-01 20:30:48 +0100293 unsigned long len = msg->body_len;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200294 const char *params = b_ptr(req->buf, (int)(msg->sov - req->buf->o));
Willy Tarreau157dd632009-12-06 19:18:09 +0100295 const char *p = params;
Bhaskar98634f02013-10-29 23:30:51 -0400296 const char *start, *end;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200297
Willy Tarreau9b28e032012-10-12 23:49:43 +0200298 if (len > buffer_len(req->buf) - msg->sov)
299 len = buffer_len(req->buf) - msg->sov;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200300
Willy Tarreau157dd632009-12-06 19:18:09 +0100301 if (len == 0)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200302 return NULL;
303
Willy Tarreau157dd632009-12-06 19:18:09 +0100304 if (px->lbprm.tot_weight == 0)
305 return NULL;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200306
307 while (len > plen) {
308 /* Look for the parameter name followed by an equal symbol */
309 if (params[plen] == '=') {
310 if (memcmp(params, px->url_param_name, plen) == 0) {
311 /* OK, we have the parameter here at <params>, and
312 * the value after the equal sign, at <p>
313 * skip the equal symbol
314 */
315 p += plen + 1;
Bhaskar98634f02013-10-29 23:30:51 -0400316 start = end = p;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200317 len -= plen + 1;
318
Bhaskar98634f02013-10-29 23:30:51 -0400319 while (len && *end != '&') {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200320 if (unlikely(!HTTP_IS_TOKEN(*p))) {
Willy Tarreau157dd632009-12-06 19:18:09 +0100321 /* if in a POST, body must be URI encoded or it's not a URI.
Bhaskar98634f02013-10-29 23:30:51 -0400322 * Do not interpret any possible binary data as a parameter.
Willy Tarreau157dd632009-12-06 19:18:09 +0100323 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200324 if (likely(HTTP_IS_LWS(*p))) /* eol, uncertain uri len */
325 break;
326 return NULL; /* oh, no; this is not uri-encoded.
327 * This body does not contain parameters.
328 */
329 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200330 len--;
Bhaskar98634f02013-10-29 23:30:51 -0400331 end++;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200332 /* should we break if vlen exceeds limit? */
333 }
Bhaskar98634f02013-10-29 23:30:51 -0400334 hash = gen_hash(px, start, (end - start));
335
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500336 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100337 hash = full_hash(hash);
Bhaskar98634f02013-10-29 23:30:51 -0400338
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200339 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
340 return chash_get_server_hash(px, hash);
341 else
342 return map_get_server_hash(px, hash);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200343 }
344 }
Willy Tarreau01732802007-11-01 22:48:15 +0100345 /* skip to next parameter */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200346 p = memchr(params, '&', len);
Willy Tarreau01732802007-11-01 22:48:15 +0100347 if (!p)
348 return NULL;
349 p++;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200350 len -= (p - params);
351 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100352 }
353 return NULL;
354}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200355
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200356
Willy Tarreaubaaee002006-06-26 02:48:02 +0200357/*
Benoitaffb4812009-03-25 13:02:10 +0100358 * This function tries to find a running server for the proxy <px> following
359 * the Header parameter hash method. It looks for a specific parameter in the
360 * URL and hashes it to compute the server ID. This is useful to optimize
361 * performance by avoiding bounces between servers in contexts where sessions
362 * are shared but cookies are not usable. If the parameter is not found, NULL
363 * is returned. If any server is found, it will be returned. If no valid server
364 * is found, NULL is returned.
365 */
366struct server *get_server_hh(struct session *s)
367{
368 unsigned long hash = 0;
369 struct http_txn *txn = &s->txn;
Benoitaffb4812009-03-25 13:02:10 +0100370 struct proxy *px = s->be;
371 unsigned int plen = px->hh_len;
372 unsigned long len;
373 struct hdr_ctx ctx;
374 const char *p;
Bhaskar98634f02013-10-29 23:30:51 -0400375 const char *start, *end;
Benoitaffb4812009-03-25 13:02:10 +0100376
377 /* tot_weight appears to mean srv_count */
378 if (px->lbprm.tot_weight == 0)
379 return NULL;
380
Benoitaffb4812009-03-25 13:02:10 +0100381 ctx.idx = 0;
382
383 /* if the message is chunked, we skip the chunk size, but use the value as len */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200384 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 +0100385
386 /* if the header is not found or empty, let's fallback to round robin */
387 if (!ctx.idx || !ctx.vlen)
388 return NULL;
389
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200390 /* note: we won't hash if there's only one server left */
391 if (px->lbprm.tot_used == 1)
392 goto hash_done;
393
Benoitaffb4812009-03-25 13:02:10 +0100394 /* Found a the hh_name in the headers.
395 * we will compute the hash based on this value ctx.val.
396 */
397 len = ctx.vlen;
398 p = (char *)ctx.line + ctx.val;
399 if (!px->hh_match_domain) {
Bhaskar98634f02013-10-29 23:30:51 -0400400 hash = gen_hash(px, p, len);
Benoitaffb4812009-03-25 13:02:10 +0100401 } else {
402 int dohash = 0;
403 p += len - 1;
Bhaskar98634f02013-10-29 23:30:51 -0400404 start = end = p;
Benoitaffb4812009-03-25 13:02:10 +0100405 /* special computation, use only main domain name, not tld/host
406 * going back from the end of string, start hashing at first
407 * dot stop at next.
408 * This is designed to work with the 'Host' header, and requires
409 * a special option to activate this.
410 */
411 while (len) {
412 if (*p == '.') {
Bhaskar98634f02013-10-29 23:30:51 -0400413 if (!dohash) {
Benoitaffb4812009-03-25 13:02:10 +0100414 dohash = 1;
Bhaskar98634f02013-10-29 23:30:51 -0400415 start = end = p - 1;
416 }
Benoitaffb4812009-03-25 13:02:10 +0100417 else
418 break;
419 } else {
420 if (dohash)
Bhaskar98634f02013-10-29 23:30:51 -0400421 start--;
Benoitaffb4812009-03-25 13:02:10 +0100422 }
423 len--;
424 p--;
425 }
Bhaskar98634f02013-10-29 23:30:51 -0400426 hash = gen_hash(px, start, (end - start));
Benoitaffb4812009-03-25 13:02:10 +0100427 }
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500428 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100429 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200430 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200431 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
432 return chash_get_server_hash(px, hash);
433 else
434 return map_get_server_hash(px, hash);
Benoitaffb4812009-03-25 13:02:10 +0100435}
436
Willy Tarreau34db1082012-04-19 17:16:54 +0200437/* RDP Cookie HASH. */
Emeric Brun736aa232009-06-30 17:56:00 +0200438struct server *get_server_rch(struct session *s)
439{
440 unsigned long hash = 0;
441 struct proxy *px = s->be;
442 unsigned long len;
Emeric Brun736aa232009-06-30 17:56:00 +0200443 int ret;
Willy Tarreau37406352012-04-23 16:16:37 +0200444 struct sample smp;
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200445 int rewind;
Emeric Brun736aa232009-06-30 17:56:00 +0200446
447 /* tot_weight appears to mean srv_count */
448 if (px->lbprm.tot_weight == 0)
449 return NULL;
450
Willy Tarreau37406352012-04-23 16:16:37 +0200451 memset(&smp, 0, sizeof(smp));
Emeric Brun736aa232009-06-30 17:56:00 +0200452
Willy Tarreau9b28e032012-10-12 23:49:43 +0200453 b_rew(s->req->buf, rewind = s->req->buf->o);
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200454
Willy Tarreaucadd8c92013-07-22 18:09:52 +0200455 ret = fetch_rdp_cookie_name(s, &smp, px->hh_name, px->hh_len);
Willy Tarreauf853c462012-04-23 18:53:56 +0200456 len = smp.data.str.len;
Willy Tarreau664092c2011-12-16 19:11:42 +0100457
Willy Tarreau9b28e032012-10-12 23:49:43 +0200458 b_adv(s->req->buf, rewind);
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200459
Willy Tarreau37406352012-04-23 16:16:37 +0200460 if (ret == 0 || (smp.flags & SMP_F_MAY_CHANGE) || len == 0)
Emeric Brun736aa232009-06-30 17:56:00 +0200461 return NULL;
462
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200463 /* note: we won't hash if there's only one server left */
464 if (px->lbprm.tot_used == 1)
465 goto hash_done;
466
Emeric Brun736aa232009-06-30 17:56:00 +0200467 /* Found a the hh_name in the headers.
468 * we will compute the hash based on this value ctx.val.
469 */
Bhaskar98634f02013-10-29 23:30:51 -0400470 hash = gen_hash(px, smp.data.str.str, len);
471
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500472 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100473 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200474 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200475 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
476 return chash_get_server_hash(px, hash);
477 else
478 return map_get_server_hash(px, hash);
Emeric Brun736aa232009-06-30 17:56:00 +0200479}
Benoitaffb4812009-03-25 13:02:10 +0100480
481/*
Willy Tarreau7c669d72008-06-20 15:04:11 +0200482 * This function applies the load-balancing algorithm to the session, as
483 * defined by the backend it is assigned to. The session is then marked as
484 * 'assigned'.
485 *
486 * This function MAY NOT be called with SN_ASSIGNED already set. If the session
487 * had a server previously assigned, it is rebalanced, trying to avoid the same
Willy Tarreau827aee92011-03-10 16:55:02 +0100488 * server, which should still be present in target_srv(&s->target) before the call.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200489 * The function tries to keep the original connection slot if it reconnects to
490 * the same server, otherwise it releases it and tries to offer it.
491 *
492 * It is illegal to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200493 *
494 * It may return :
Willy Tarreau664beb82011-03-10 11:38:29 +0100495 * SRV_STATUS_OK if everything is OK. ->srv and ->target are assigned.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200496 * SRV_STATUS_NOSRV if no server is available. Session is not ASSIGNED
497 * SRV_STATUS_FULL if all servers are saturated. Session is not ASSIGNED
Willy Tarreaubaaee002006-06-26 02:48:02 +0200498 * SRV_STATUS_INTERNAL for other unrecoverable errors.
499 *
Willy Tarreau7c669d72008-06-20 15:04:11 +0200500 * Upon successful return, the session flag SN_ASSIGNED is set to indicate that
Willy Tarreau827aee92011-03-10 16:55:02 +0100501 * it does not need to be called anymore. This means that target_srv(&s->target)
502 * can be trusted in balance and direct modes.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200503 *
504 */
505
506int assign_server(struct session *s)
507{
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200508 struct connection *conn;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200509 struct server *conn_slot;
Willy Tarreau827aee92011-03-10 16:55:02 +0100510 struct server *srv, *prev_srv;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200511 int err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100512
Simon Horman8effd3d2011-08-13 08:03:52 +0900513 DPRINTF(stderr,"assign_server : s=%p\n",s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200514
Willy Tarreau7c669d72008-06-20 15:04:11 +0200515 err = SRV_STATUS_INTERNAL;
516 if (unlikely(s->pend_pos || s->flags & SN_ASSIGNED))
517 goto out_err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100518
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100519 prev_srv = objt_server(s->target);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200520 conn_slot = s->srv_conn;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200521
Willy Tarreau7c669d72008-06-20 15:04:11 +0200522 /* We have to release any connection slot before applying any LB algo,
523 * otherwise we may erroneously end up with no available slot.
524 */
525 if (conn_slot)
526 sess_change_server(s, NULL);
527
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100528 /* We will now try to find the good server and store it into <objt_server(s->target)>.
529 * Note that <objt_server(s->target)> may be NULL in case of dispatch or proxy mode,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200530 * as well as if no server is available (check error code).
531 */
Willy Tarreau1a20a5d2007-11-01 21:08:19 +0100532
Willy Tarreau827aee92011-03-10 16:55:02 +0100533 srv = NULL;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100534 s->target = NULL;
Willy Tarreau9420b122013-12-15 18:58:25 +0100535 conn = objt_conn(s->req->cons->end);
Willy Tarreau664beb82011-03-10 11:38:29 +0100536
Willy Tarreau068621e2013-12-23 15:11:25 +0100537 if (conn &&
538 ((s->be->options & PR_O_PREF_LAST) || (s->txn.flags & TX_PREFER_LAST)) &&
Willy Tarreau9420b122013-12-15 18:58:25 +0100539 objt_server(conn->target) && __objt_server(conn->target)->proxy == s->be &&
540 srv_is_usable(__objt_server(conn->target)->state, __objt_server(conn->target)->eweight)) {
541 /* This session was relying on a server in a previous request
542 * and the proxy has "option prefer-current-server" set, so
543 * let's try to reuse the same server.
544 */
545 srv = __objt_server(conn->target);
546 s->target = &srv->obj_type;
547 }
548 else if (s->be->lbprm.algo & BE_LB_KIND) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200549 /* we must check if we have at least one server available */
550 if (!s->be->lbprm.tot_weight) {
551 err = SRV_STATUS_NOSRV;
552 goto out;
553 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200554
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200555 /* First check whether we need to fetch some data or simply call
556 * the LB lookup function. Only the hashing functions will need
557 * some input data in fact, and will support multiple algorithms.
558 */
559 switch (s->be->lbprm.algo & BE_LB_LKUP) {
560 case BE_LB_LKUP_RRTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100561 srv = fwrr_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200562 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200563
Willy Tarreauf09c6602012-02-13 17:12:08 +0100564 case BE_LB_LKUP_FSTREE:
565 srv = fas_get_next_server(s->be, prev_srv);
566 break;
567
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200568 case BE_LB_LKUP_LCTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100569 srv = fwlc_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200570 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200571
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200572 case BE_LB_LKUP_CHTREE:
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200573 case BE_LB_LKUP_MAP:
Willy Tarreau9757a382009-10-03 12:56:50 +0200574 if ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200575 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100576 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200577 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100578 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau9757a382009-10-03 12:56:50 +0200579 break;
580 }
581 else if ((s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_HI) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200582 /* unknown balancing algorithm */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200583 err = SRV_STATUS_INTERNAL;
584 goto out;
585 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200586
587 switch (s->be->lbprm.algo & BE_LB_PARM) {
588 case BE_LB_HASH_SRC:
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200589 conn = objt_conn(s->req->prod->end);
590 if (conn && conn->addr.from.ss_family == AF_INET) {
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200591 srv = get_server_sh(s->be,
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200592 (void *)&((struct sockaddr_in *)&conn->addr.from)->sin_addr,
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200593 4);
594 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200595 else if (conn && conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200596 srv = get_server_sh(s->be,
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200597 (void *)&((struct sockaddr_in6 *)&conn->addr.from)->sin6_addr,
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200598 16);
599 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200600 else {
601 /* unknown IP family */
602 err = SRV_STATUS_INTERNAL;
603 goto out;
604 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200605 break;
606
607 case BE_LB_HASH_URI:
608 /* URI hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100609 if (s->txn.req.msg_state < HTTP_MSG_BODY)
610 break;
Willy Tarreau827aee92011-03-10 16:55:02 +0100611 srv = get_server_uh(s->be,
Willy Tarreau9b28e032012-10-12 23:49:43 +0200612 b_ptr(s->req->buf, (int)(s->txn.req.sl.rq.u - s->req->buf->o)),
Willy Tarreau827aee92011-03-10 16:55:02 +0100613 s->txn.req.sl.rq.u_l);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200614 break;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200615
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200616 case BE_LB_HASH_PRM:
617 /* URL Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100618 if (s->txn.req.msg_state < HTTP_MSG_BODY)
619 break;
Willy Tarreau61a21a32011-03-01 20:35:49 +0100620
Willy Tarreau827aee92011-03-10 16:55:02 +0100621 srv = get_server_ph(s->be,
Willy Tarreau9b28e032012-10-12 23:49:43 +0200622 b_ptr(s->req->buf, (int)(s->txn.req.sl.rq.u - s->req->buf->o)),
Willy Tarreau827aee92011-03-10 16:55:02 +0100623 s->txn.req.sl.rq.u_l);
Willy Tarreau61a21a32011-03-01 20:35:49 +0100624
Willy Tarreau827aee92011-03-10 16:55:02 +0100625 if (!srv && s->txn.meth == HTTP_METH_POST)
626 srv = get_server_ph_post(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200627 break;
Benoitaffb4812009-03-25 13:02:10 +0100628
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200629 case BE_LB_HASH_HDR:
630 /* Header Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100631 if (s->txn.req.msg_state < HTTP_MSG_BODY)
632 break;
Willy Tarreau827aee92011-03-10 16:55:02 +0100633 srv = get_server_hh(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200634 break;
635
636 case BE_LB_HASH_RDP:
637 /* RDP Cookie hashing */
Willy Tarreau827aee92011-03-10 16:55:02 +0100638 srv = get_server_rch(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200639 break;
640
641 default:
642 /* unknown balancing algorithm */
643 err = SRV_STATUS_INTERNAL;
644 goto out;
Benoitaffb4812009-03-25 13:02:10 +0100645 }
Emeric Brun736aa232009-06-30 17:56:00 +0200646
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200647 /* If the hashing parameter was not found, let's fall
648 * back to round robin on the map.
649 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100650 if (!srv) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200651 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100652 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200653 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100654 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200655 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200656
657 /* end of map-based LB */
Emeric Brun736aa232009-06-30 17:56:00 +0200658 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200659
Willy Tarreau7c669d72008-06-20 15:04:11 +0200660 default:
661 /* unknown balancing algorithm */
662 err = SRV_STATUS_INTERNAL;
663 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200664 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200665
Willy Tarreau827aee92011-03-10 16:55:02 +0100666 if (!srv) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200667 err = SRV_STATUS_FULL;
668 goto out;
669 }
Willy Tarreau827aee92011-03-10 16:55:02 +0100670 else if (srv != prev_srv) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100671 s->be->be_counters.cum_lbconn++;
Willy Tarreau827aee92011-03-10 16:55:02 +0100672 srv->counters.cum_lbconn++;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100673 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100674 s->target = &srv->obj_type;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200675 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200676 else if (s->be->options & (PR_O_DISPATCH | PR_O_TRANSP)) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100677 s->target = &s->be->obj_type;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200678 }
David du Colombier6f5ccb12011-03-10 22:26:24 +0100679 else if ((s->be->options & PR_O_HTTP_PROXY) &&
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200680 (conn = objt_conn(s->req->cons->end)) &&
681 is_addr(&conn->addr.to)) {
Willy Tarreau664beb82011-03-10 11:38:29 +0100682 /* in proxy mode, we need a valid destination address */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100683 s->target = &s->be->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +0100684 }
685 else {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200686 err = SRV_STATUS_NOSRV;
687 goto out;
688 }
689
690 s->flags |= SN_ASSIGNED;
691 err = SRV_STATUS_OK;
692 out:
693
694 /* Either we take back our connection slot, or we offer it to someone
695 * else if we don't need it anymore.
696 */
697 if (conn_slot) {
Willy Tarreau827aee92011-03-10 16:55:02 +0100698 if (conn_slot == srv) {
699 sess_change_server(s, srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200700 } else {
701 if (may_dequeue_tasks(conn_slot, s->be))
702 process_srv_queue(conn_slot);
703 }
704 }
705
706 out_err:
707 return err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200708}
709
710
711/*
712 * This function assigns a server address to a session, and sets SN_ADDR_SET.
713 * The address is taken from the currently assigned server, or from the
714 * dispatch or transparent address.
715 *
716 * It may return :
717 * SRV_STATUS_OK if everything is OK.
718 * SRV_STATUS_INTERNAL for other unrecoverable errors.
719 *
720 * Upon successful return, the session flag SN_ADDR_SET is set. This flag is
721 * not cleared, so it's to the caller to clear it if required.
722 *
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200723 * The caller is responsible for having already assigned a connection
724 * to si->end.
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200725 *
Willy Tarreaubaaee002006-06-26 02:48:02 +0200726 */
727int assign_server_address(struct session *s)
728{
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200729 struct connection *cli_conn = objt_conn(s->req->prod->end);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200730 struct connection *srv_conn = objt_conn(s->req->cons->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200731
Willy Tarreaubaaee002006-06-26 02:48:02 +0200732#ifdef DEBUG_FULL
733 fprintf(stderr,"assign_server_address : s=%p\n",s);
734#endif
735
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200736 if ((s->flags & SN_DIRECT) || (s->be->lbprm.algo & BE_LB_KIND)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200737 /* A server is necessarily known for this session */
738 if (!(s->flags & SN_ASSIGNED))
739 return SRV_STATUS_INTERNAL;
740
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200741 srv_conn->addr.to = objt_server(s->target)->addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200742
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200743 if (!is_addr(&srv_conn->addr.to) && cli_conn) {
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200744 /* if the server has no address, we use the same address
745 * the client asked, which is handy for remapping ports
746 * locally on multiple addresses at once.
747 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200748 conn_get_to_addr(cli_conn);
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200749
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200750 if (cli_conn->addr.to.ss_family == AF_INET) {
751 ((struct sockaddr_in *)&srv_conn->addr.to)->sin_addr = ((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
752 } else if (cli_conn->addr.to.ss_family == AF_INET6) {
753 ((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 +0200754 }
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200755 }
756
Willy Tarreaubaaee002006-06-26 02:48:02 +0200757 /* if this server remaps proxied ports, we'll use
758 * the port the client connected to with an offset. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200759 if ((objt_server(s->target)->state & SRV_MAPPORTS) && cli_conn) {
David du Colombier6f5ccb12011-03-10 22:26:24 +0100760 int base_port;
761
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200762 conn_get_to_addr(cli_conn);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100763
764 /* First, retrieve the port from the incoming connection */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200765 base_port = get_host_port(&cli_conn->addr.to);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100766
767 /* Second, assign the outgoing connection's port */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200768 base_port += get_host_port(&srv_conn->addr.to);
769 set_host_port(&srv_conn->addr.to, base_port);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200770 }
771 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200772 else if (s->be->options & PR_O_DISPATCH) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200773 /* connect to the defined dispatch addr */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200774 srv_conn->addr.to = s->be->dispatch_addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200775 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200776 else if ((s->be->options & PR_O_TRANSP) && cli_conn) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200777 /* in transparent mode, use the original dest addr if no dispatch specified */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200778 conn_get_to_addr(cli_conn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200779
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200780 if (cli_conn->addr.to.ss_family == AF_INET || cli_conn->addr.to.ss_family == AF_INET6)
781 srv_conn->addr.to = cli_conn->addr.to;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200782 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100783 else if (s->be->options & PR_O_HTTP_PROXY) {
784 /* If HTTP PROXY option is set, then server is already assigned
785 * during incoming client request parsing. */
786 }
Willy Tarreau1a1158b2007-01-20 11:07:46 +0100787 else {
788 /* no server and no LB algorithm ! */
789 return SRV_STATUS_INTERNAL;
790 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200791
792 s->flags |= SN_ADDR_SET;
793 return SRV_STATUS_OK;
794}
795
796
797/* This function assigns a server to session <s> if required, and can add the
798 * connection to either the assigned server's queue or to the proxy's queue.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200799 * If ->srv_conn is set, the session is first released from the server.
800 * It may also be called with SN_DIRECT and/or SN_ASSIGNED though. It will
801 * be called before any connection and after any retry or redispatch occurs.
802 *
803 * It is not allowed to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200804 *
805 * Returns :
806 *
807 * SRV_STATUS_OK if everything is OK.
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100808 * SRV_STATUS_NOSRV if no server is available. objt_server(s->target) = NULL.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200809 * SRV_STATUS_QUEUED if the connection has been queued.
810 * SRV_STATUS_FULL if the server(s) is/are saturated and the
Willy Tarreau827aee92011-03-10 16:55:02 +0100811 * connection could not be queued at the server's,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200812 * which may be NULL if we queue on the backend.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200813 * SRV_STATUS_INTERNAL for other unrecoverable errors.
814 *
815 */
816int assign_server_and_queue(struct session *s)
817{
818 struct pendconn *p;
Willy Tarreau827aee92011-03-10 16:55:02 +0100819 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200820 int err;
821
822 if (s->pend_pos)
823 return SRV_STATUS_INTERNAL;
824
Willy Tarreau7c669d72008-06-20 15:04:11 +0200825 err = SRV_STATUS_OK;
826 if (!(s->flags & SN_ASSIGNED)) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100827 struct server *prev_srv = objt_server(s->target);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100828
Willy Tarreau7c669d72008-06-20 15:04:11 +0200829 err = assign_server(s);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100830 if (prev_srv) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200831 /* This session was previously assigned to a server. We have to
832 * update the session's and the server's stats :
833 * - if the server changed :
834 * - set TX_CK_DOWN if txn.flags was TX_CK_VALID
835 * - set SN_REDISP if it was successfully redispatched
836 * - increment srv->redispatches and be->redispatches
837 * - if the server remained the same : update retries.
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100838 */
839
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100840 if (prev_srv != objt_server(s->target)) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200841 if ((s->txn.flags & TX_CK_MASK) == TX_CK_VALID) {
842 s->txn.flags &= ~TX_CK_MASK;
843 s->txn.flags |= TX_CK_DOWN;
844 }
845 s->flags |= SN_REDISP;
Willy Tarreau3d80d912011-03-10 11:42:13 +0100846 prev_srv->counters.redispatches++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100847 s->be->be_counters.redispatches++;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200848 } else {
Willy Tarreau3d80d912011-03-10 11:42:13 +0100849 prev_srv->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100850 s->be->be_counters.retries++;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100851 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100852 }
853 }
854
Willy Tarreaubaaee002006-06-26 02:48:02 +0200855 switch (err) {
856 case SRV_STATUS_OK:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200857 /* we have SN_ASSIGNED set */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100858 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100859 if (!srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200860 return SRV_STATUS_OK; /* dispatch or proxy mode */
861
862 /* If we already have a connection slot, no need to check any queue */
Willy Tarreau827aee92011-03-10 16:55:02 +0100863 if (s->srv_conn == srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200864 return SRV_STATUS_OK;
865
866 /* OK, this session already has an assigned server, but no
867 * connection slot yet. Either it is a redispatch, or it was
868 * assigned from persistence information (direct mode).
869 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100870 if ((s->flags & SN_REDIRECTABLE) && srv->rdr_len) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200871 /* server scheduled for redirection, and already assigned. We
872 * don't want to go further nor check the queue.
Willy Tarreau21d2af32008-02-14 20:25:24 +0100873 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100874 sess_change_server(s, srv); /* not really needed in fact */
Willy Tarreau21d2af32008-02-14 20:25:24 +0100875 return SRV_STATUS_OK;
876 }
877
Willy Tarreau7c669d72008-06-20 15:04:11 +0200878 /* We might have to queue this session if the assigned server is full.
879 * We know we have to queue it into the server's queue, so if a maxqueue
880 * is set on the server, we must also check that the server's queue is
881 * not full, in which case we have to return FULL.
882 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100883 if (srv->maxconn &&
884 (srv->nbpend || srv->served >= srv_dynamic_maxconn(srv))) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200885
Willy Tarreau827aee92011-03-10 16:55:02 +0100886 if (srv->maxqueue > 0 && srv->nbpend >= srv->maxqueue)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200887 return SRV_STATUS_FULL;
888
Willy Tarreaubaaee002006-06-26 02:48:02 +0200889 p = pendconn_add(s);
890 if (p)
891 return SRV_STATUS_QUEUED;
892 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200893 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200894 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200895
896 /* OK, we can use this server. Let's reserve our place */
Willy Tarreau827aee92011-03-10 16:55:02 +0100897 sess_change_server(s, srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200898 return SRV_STATUS_OK;
899
900 case SRV_STATUS_FULL:
901 /* queue this session into the proxy's queue */
902 p = pendconn_add(s);
903 if (p)
904 return SRV_STATUS_QUEUED;
905 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200906 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200907
908 case SRV_STATUS_NOSRV:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200909 return err;
910
Willy Tarreaubaaee002006-06-26 02:48:02 +0200911 case SRV_STATUS_INTERNAL:
912 return err;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200913
Willy Tarreaubaaee002006-06-26 02:48:02 +0200914 default:
915 return SRV_STATUS_INTERNAL;
916 }
Willy Tarreau5b6995c2008-01-13 16:31:17 +0100917}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200918
Willy Tarreaub1d67742010-03-29 19:36:59 +0200919/* If an explicit source binding is specified on the server and/or backend, and
920 * this source makes use of the transparent proxy, then it is extracted now and
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200921 * assigned to the session's pending connection. This function assumes that an
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200922 * outgoing connection has already been assigned to s->req->cons->end.
Willy Tarreaub1d67742010-03-29 19:36:59 +0200923 */
924static void assign_tproxy_address(struct session *s)
925{
Pieter Baauwd551fb52013-05-08 22:49:23 +0200926#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_TRANSPARENT)
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100927 struct server *srv = objt_server(s->target);
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100928 struct conn_src *src;
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200929 struct connection *cli_conn;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200930 struct connection *srv_conn = objt_conn(s->req->cons->end);
Willy Tarreau827aee92011-03-10 16:55:02 +0100931
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100932 if (srv && srv->conn_src.opts & CO_SRC_BIND)
933 src = &srv->conn_src;
934 else if (s->be->conn_src.opts & CO_SRC_BIND)
935 src = &s->be->conn_src;
936 else
937 return;
Willy Tarreau294c4732011-12-16 21:35:50 +0100938
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100939 switch (src->opts & CO_SRC_TPROXY_MASK) {
940 case CO_SRC_TPROXY_ADDR:
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200941 srv_conn->addr.from = src->tproxy_addr;
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100942 break;
943 case CO_SRC_TPROXY_CLI:
944 case CO_SRC_TPROXY_CIP:
945 /* FIXME: what can we do if the client connects in IPv6 or unix socket ? */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200946 cli_conn = objt_conn(s->req->prod->end);
947 if (cli_conn)
948 srv_conn->addr.from = cli_conn->addr.from;
949 else
950 memset(&srv_conn->addr.from, 0, sizeof(srv_conn->addr.from));
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100951 break;
952 case CO_SRC_TPROXY_DYN:
953 if (src->bind_hdr_occ) {
954 char *vptr;
955 int vlen;
956 int rewind;
Willy Tarreau294c4732011-12-16 21:35:50 +0100957
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100958 /* bind to the IP in a header */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200959 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_family = AF_INET;
960 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_port = 0;
961 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_addr.s_addr = 0;
Willy Tarreau294c4732011-12-16 21:35:50 +0100962
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100963 b_rew(s->req->buf, rewind = s->req->buf->o);
964 if (http_get_hdr(&s->txn.req, src->bind_hdr_name, src->bind_hdr_len,
965 &s->txn.hdr_idx, src->bind_hdr_occ, NULL, &vptr, &vlen)) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200966 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_addr.s_addr =
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100967 htonl(inetaddr_host_lim(vptr, vptr + vlen));
Willy Tarreaubce70882009-09-07 11:51:47 +0200968 }
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100969 b_adv(s->req->buf, rewind);
Willy Tarreaub1d67742010-03-29 19:36:59 +0200970 }
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100971 break;
972 default:
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200973 memset(&srv_conn->addr.from, 0, sizeof(srv_conn->addr.from));
Willy Tarreaub1d67742010-03-29 19:36:59 +0200974 }
975#endif
976}
977
978
Willy Tarreaubaaee002006-06-26 02:48:02 +0200979/*
980 * This function initiates a connection to the server assigned to this session
Willy Tarreau6471afb2011-09-23 10:54:59 +0200981 * (s->target, s->req->cons->addr.to). It will assign a server if none
Willy Tarreau664beb82011-03-10 11:38:29 +0100982 * is assigned yet.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200983 * It can return one of :
984 * - SN_ERR_NONE if everything's OK
985 * - SN_ERR_SRVTO if there are no more servers
986 * - SN_ERR_SRVCL if the connection was refused by the server
987 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
988 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
989 * - SN_ERR_INTERNAL for any other purely internal errors
990 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200991 * The server-facing stream interface is expected to hold a pre-allocated connection
992 * in s->req->cons->conn.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200993 */
994int connect_server(struct session *s)
995{
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200996 struct connection *cli_conn;
Willy Tarreau34601a82013-12-15 16:33:46 +0100997 struct connection *srv_conn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100998 struct server *srv;
Willy Tarreau34601a82013-12-15 16:33:46 +0100999 int reuse = 0;
Willy Tarreau9650f372009-08-16 14:02:45 +02001000 int err;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001001
Willy Tarreau34601a82013-12-15 16:33:46 +01001002 srv_conn = objt_conn(s->req->cons->end);
1003 if (srv_conn)
1004 reuse = s->target == srv_conn->target;
1005
1006 if (reuse) {
1007 /* Disable connection reuse if a dynamic source is used.
1008 * As long as we don't share connections between servers,
1009 * we don't need to disable connection reuse on no-idempotent
1010 * requests nor when PROXY protocol is used.
1011 */
1012 srv = objt_server(s->target);
1013 if (srv && srv->conn_src.opts & CO_SRC_BIND) {
1014 if ((srv->conn_src.opts & CO_SRC_TPROXY_MASK) == CO_SRC_TPROXY_DYN)
1015 reuse = 0;
1016 }
1017 else if (s->be->conn_src.opts & CO_SRC_BIND) {
1018 if ((s->be->conn_src.opts & CO_SRC_TPROXY_MASK) == CO_SRC_TPROXY_DYN)
1019 reuse = 0;
1020 }
1021 }
1022
1023 srv_conn = si_alloc_conn(s->req->cons, reuse);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001024 if (!srv_conn)
1025 return SN_ERR_RESOURCE;
1026
Willy Tarreaubaaee002006-06-26 02:48:02 +02001027 if (!(s->flags & SN_ADDR_SET)) {
1028 err = assign_server_address(s);
1029 if (err != SRV_STATUS_OK)
1030 return SN_ERR_INTERNAL;
1031 }
1032
Willy Tarreauff605db2013-12-20 11:09:51 +01001033 if (!conn_xprt_ready(srv_conn)) {
1034 /* the target was only on the session, assign it to the SI now */
1035 srv_conn->target = s->target;
Willy Tarreaud88edf22009-06-14 15:48:17 +02001036
Willy Tarreauff605db2013-12-20 11:09:51 +01001037 /* set the correct protocol on the output stream interface */
1038 if (objt_server(s->target)) {
1039 conn_prepare(srv_conn, objt_server(s->target)->proto, objt_server(s->target)->xprt);
1040 }
1041 else if (obj_type(s->target) == OBJ_TYPE_PROXY) {
1042 /* proxies exclusively run on raw_sock right now */
1043 conn_prepare(srv_conn, protocol_by_family(srv_conn->addr.to.ss_family), &raw_sock);
1044 if (!objt_conn(s->req->cons->end) || !objt_conn(s->req->cons->end)->ctrl)
1045 return SN_ERR_INTERNAL;
1046 }
1047 else
1048 return SN_ERR_INTERNAL; /* how did we get there ? */
Willy Tarreaud02394b2012-05-11 18:32:18 +02001049
Willy Tarreauff605db2013-12-20 11:09:51 +01001050 /* process the case where the server requires the PROXY protocol to be sent */
1051 srv_conn->send_proxy_ofs = 0;
1052 if (objt_server(s->target) && (objt_server(s->target)->state & SRV_SEND_PROXY)) {
1053 srv_conn->send_proxy_ofs = 1; /* must compute size */
1054 cli_conn = objt_conn(s->req->prod->end);
1055 if (cli_conn)
1056 conn_get_to_addr(cli_conn);
1057 }
Willy Tarreau2a6e8802013-10-24 15:50:53 +02001058
Willy Tarreauff605db2013-12-20 11:09:51 +01001059 si_attach_conn(s->req->cons, srv_conn);
Willy Tarreau26d8c592012-05-07 18:12:14 +02001060
Willy Tarreauff605db2013-12-20 11:09:51 +01001061 assign_tproxy_address(s);
1062 }
1063 else {
1064 /* the connection is being reused, just re-attach it */
1065 si_attach_conn(s->req->cons, srv_conn);
1066 }
Willy Tarreaub1d67742010-03-29 19:36:59 +02001067
William Lallemandb7ff6a32012-03-02 14:35:21 +01001068 /* flag for logging source ip/port */
1069 if (s->fe->options2 & PR_O2_SRC_ADDR)
1070 s->req->cons->flags |= SI_FL_SRC_ADDR;
1071
Willy Tarreau14f8e862012-08-30 22:23:13 +02001072 /* disable lingering */
1073 if (s->be->options & PR_O_TCP_NOLING)
1074 s->req->cons->flags |= SI_FL_NOLINGER;
1075
Willy Tarreau73b013b2012-05-21 16:31:45 +02001076 err = si_connect(s->req->cons);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001077
Willy Tarreau9650f372009-08-16 14:02:45 +02001078 if (err != SN_ERR_NONE)
1079 return err;
Willy Tarreau788e2842008-08-26 13:25:39 +02001080
Willy Tarreau14f8e862012-08-30 22:23:13 +02001081 /* set connect timeout */
1082 s->req->cons->exp = tick_add_ifset(now_ms, s->be->timeout.connect);
1083
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001084 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001085 if (srv) {
Willy Tarreau1e62de62008-11-11 20:20:02 +01001086 s->flags |= SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001087 srv->cur_sess++;
1088 if (srv->cur_sess > srv->counters.cur_sess_max)
1089 srv->counters.cur_sess_max = srv->cur_sess;
Willy Tarreau51406232008-03-10 22:04:20 +01001090 if (s->be->lbprm.server_take_conn)
Willy Tarreau827aee92011-03-10 16:55:02 +01001091 s->be->lbprm.server_take_conn(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001092 }
1093
Willy Tarreaubaaee002006-06-26 02:48:02 +02001094 return SN_ERR_NONE; /* connection is OK */
1095}
1096
1097
Willy Tarreaubaaee002006-06-26 02:48:02 +02001098/* This function performs the "redispatch" part of a connection attempt. It
1099 * will assign a server if required, queue the connection if required, and
1100 * handle errors that might arise at this level. It can change the server
1101 * state. It will return 1 if it encounters an error, switches the server
1102 * state, or has to queue a connection. Otherwise, it will return 0 indicating
1103 * that the connection is ready to use.
1104 */
1105
1106int srv_redispatch_connect(struct session *t)
1107{
Willy Tarreau827aee92011-03-10 16:55:02 +01001108 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001109 int conn_err;
1110
1111 /* We know that we don't have any connection pending, so we will
1112 * try to get a new one, and wait in this state if it's queued
1113 */
Willy Tarreau7c669d72008-06-20 15:04:11 +02001114 redispatch:
Willy Tarreaubaaee002006-06-26 02:48:02 +02001115 conn_err = assign_server_and_queue(t);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001116 srv = objt_server(t->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001117
Willy Tarreaubaaee002006-06-26 02:48:02 +02001118 switch (conn_err) {
1119 case SRV_STATUS_OK:
1120 break;
1121
Willy Tarreau7c669d72008-06-20 15:04:11 +02001122 case SRV_STATUS_FULL:
1123 /* The server has reached its maxqueue limit. Either PR_O_REDISP is set
1124 * and we can redispatch to another server, or it is not and we return
1125 * 503. This only makes sense in DIRECT mode however, because normal LB
1126 * algorithms would never select such a server, and hash algorithms
Willy Tarreau827aee92011-03-10 16:55:02 +01001127 * would bring us on the same server again. Note that t->target is set
1128 * in this case.
Willy Tarreau7c669d72008-06-20 15:04:11 +02001129 */
Willy Tarreau4de91492010-01-22 19:10:05 +01001130 if (((t->flags & (SN_DIRECT|SN_FORCE_PRST)) == SN_DIRECT) &&
1131 (t->be->options & PR_O_REDISP)) {
Willy Tarreau7c669d72008-06-20 15:04:11 +02001132 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Willy Tarreau7c669d72008-06-20 15:04:11 +02001133 goto redispatch;
1134 }
1135
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001136 if (!t->req->cons->err_type) {
1137 t->req->cons->err_type = SI_ET_QUEUE_ERR;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001138 }
Willy Tarreau7c669d72008-06-20 15:04:11 +02001139
Willy Tarreau827aee92011-03-10 16:55:02 +01001140 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001141 t->be->be_counters.failed_conns++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02001142 return 1;
1143
Willy Tarreaubaaee002006-06-26 02:48:02 +02001144 case SRV_STATUS_NOSRV:
Willy Tarreau827aee92011-03-10 16:55:02 +01001145 /* note: it is guaranteed that srv == NULL here */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001146 if (!t->req->cons->err_type) {
1147 t->req->cons->err_type = SI_ET_CONN_ERR;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001148 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +01001149
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001150 t->be->be_counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001151 return 1;
1152
1153 case SRV_STATUS_QUEUED:
Willy Tarreau35374672008-09-03 18:11:02 +02001154 t->req->cons->exp = tick_add_ifset(now_ms, t->be->timeout.queue);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001155 t->req->cons->state = SI_ST_QUE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001156 /* do nothing else and do not wake any other session up */
1157 return 1;
1158
Willy Tarreaubaaee002006-06-26 02:48:02 +02001159 case SRV_STATUS_INTERNAL:
1160 default:
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001161 if (!t->req->cons->err_type) {
1162 t->req->cons->err_type = SI_ET_CONN_OTHER;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001163 }
1164
Willy Tarreau827aee92011-03-10 16:55:02 +01001165 if (srv)
1166 srv_inc_sess_ctr(srv);
1167 if (srv)
1168 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001169 t->be->be_counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001170
1171 /* release other sessions waiting for this server */
Willy Tarreau827aee92011-03-10 16:55:02 +01001172 if (may_dequeue_tasks(srv, t->be))
1173 process_srv_queue(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001174 return 1;
1175 }
1176 /* if we get here, it's because we got SRV_STATUS_OK, which also
1177 * means that the connection has not been queued.
1178 */
1179 return 0;
1180}
1181
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001182/* Apply RDP cookie persistence to the current session. For this, the function
1183 * tries to extract an RDP cookie from the request buffer, and look for the
1184 * matching server in the list. If the server is found, it is assigned to the
1185 * session. This always returns 1, and the analyser removes itself from the
1186 * list. Nothing is performed if a server was already assigned.
1187 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02001188int tcp_persist_rdp_cookie(struct session *s, struct channel *req, int an_bit)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001189{
1190 struct proxy *px = s->be;
1191 int ret;
Willy Tarreau37406352012-04-23 16:16:37 +02001192 struct sample smp;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001193 struct server *srv = px->srv;
1194 struct sockaddr_in addr;
1195 char *p;
1196
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001197 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 +02001198 now_ms, __FUNCTION__,
1199 s,
1200 req,
1201 req->rex, req->wex,
1202 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001203 req->buf->i,
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001204 req->analysers);
1205
1206 if (s->flags & SN_ASSIGNED)
1207 goto no_cookie;
1208
Willy Tarreau37406352012-04-23 16:16:37 +02001209 memset(&smp, 0, sizeof(smp));
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001210
Willy Tarreaucadd8c92013-07-22 18:09:52 +02001211 ret = fetch_rdp_cookie_name(s, &smp, s->be->rdp_cookie_name, s->be->rdp_cookie_len);
Willy Tarreauf853c462012-04-23 18:53:56 +02001212 if (ret == 0 || (smp.flags & SMP_F_MAY_CHANGE) || smp.data.str.len == 0)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001213 goto no_cookie;
1214
1215 memset(&addr, 0, sizeof(addr));
1216 addr.sin_family = AF_INET;
1217
Willy Tarreau664092c2011-12-16 19:11:42 +01001218 /* Considering an rdp cookie detected using acl, str ended with <cr><lf> and should return */
Willy Tarreauf853c462012-04-23 18:53:56 +02001219 addr.sin_addr.s_addr = strtoul(smp.data.str.str, &p, 10);
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001220 if (*p != '.')
1221 goto no_cookie;
1222 p++;
1223 addr.sin_port = (unsigned short)strtoul(p, &p, 10);
1224 if (*p != '.')
1225 goto no_cookie;
1226
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001227 s->target = NULL;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001228 while (srv) {
1229 if (memcmp(&addr, &(srv->addr), sizeof(addr)) == 0) {
1230 if ((srv->state & SRV_RUNNING) || (px->options & PR_O_PERSIST)) {
1231 /* we found the server and it is usable */
1232 s->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001233 s->target = &srv->obj_type;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001234 break;
1235 }
1236 }
1237 srv = srv->next;
1238 }
1239
1240no_cookie:
1241 req->analysers &= ~an_bit;
1242 req->analyse_exp = TICK_ETERNITY;
1243 return 1;
1244}
1245
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001246int be_downtime(struct proxy *px) {
Willy Tarreaub625a082007-11-26 01:15:43 +01001247 if (px->lbprm.tot_weight && px->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001248 return px->down_time;
1249
1250 return now.tv_sec - px->last_change + px->down_time;
1251}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001252
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001253/*
1254 * This function returns a string containing the balancing
1255 * mode of the proxy in a format suitable for stats.
1256 */
1257
1258const char *backend_lb_algo_str(int algo) {
1259
1260 if (algo == BE_LB_ALGO_RR)
1261 return "roundrobin";
1262 else if (algo == BE_LB_ALGO_SRR)
1263 return "static-rr";
Willy Tarreauf09c6602012-02-13 17:12:08 +01001264 else if (algo == BE_LB_ALGO_FAS)
1265 return "first";
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001266 else if (algo == BE_LB_ALGO_LC)
1267 return "leastconn";
1268 else if (algo == BE_LB_ALGO_SH)
1269 return "source";
1270 else if (algo == BE_LB_ALGO_UH)
1271 return "uri";
1272 else if (algo == BE_LB_ALGO_PH)
1273 return "url_param";
1274 else if (algo == BE_LB_ALGO_HH)
1275 return "hdr";
1276 else if (algo == BE_LB_ALGO_RCH)
1277 return "rdp-cookie";
1278 else
1279 return NULL;
1280}
1281
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001282/* This function parses a "balance" statement in a backend section describing
1283 * <curproxy>. It returns -1 if there is any error, otherwise zero. If it
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001284 * returns -1, it will write an error message into the <err> buffer which will
1285 * automatically be allocated and must be passed as NULL. The trailing '\n'
1286 * will not be written. The function must be called with <args> pointing to the
1287 * first word after "balance".
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001288 */
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001289int backend_parse_balance(const char **args, char **err, struct proxy *curproxy)
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001290{
1291 if (!*(args[0])) {
1292 /* if no option is set, use round-robin by default */
Willy Tarreau31682232007-11-29 15:38:04 +01001293 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1294 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001295 return 0;
1296 }
1297
1298 if (!strcmp(args[0], "roundrobin")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001299 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1300 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001301 }
Willy Tarreau9757a382009-10-03 12:56:50 +02001302 else if (!strcmp(args[0], "static-rr")) {
1303 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1304 curproxy->lbprm.algo |= BE_LB_ALGO_SRR;
1305 }
Willy Tarreauf09c6602012-02-13 17:12:08 +01001306 else if (!strcmp(args[0], "first")) {
1307 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1308 curproxy->lbprm.algo |= BE_LB_ALGO_FAS;
1309 }
Willy Tarreau51406232008-03-10 22:04:20 +01001310 else if (!strcmp(args[0], "leastconn")) {
1311 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1312 curproxy->lbprm.algo |= BE_LB_ALGO_LC;
1313 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001314 else if (!strcmp(args[0], "source")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001315 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1316 curproxy->lbprm.algo |= BE_LB_ALGO_SH;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001317 }
1318 else if (!strcmp(args[0], "uri")) {
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001319 int arg = 1;
1320
Willy Tarreau31682232007-11-29 15:38:04 +01001321 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1322 curproxy->lbprm.algo |= BE_LB_ALGO_UH;
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001323
Oskar Stolc8dc41842012-05-19 10:19:54 +01001324 curproxy->uri_whole = 0;
1325
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001326 while (*args[arg]) {
1327 if (!strcmp(args[arg], "len")) {
1328 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001329 memprintf(err, "%s : '%s' expects a positive integer (got '%s').", args[0], args[arg], args[arg+1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001330 return -1;
1331 }
1332 curproxy->uri_len_limit = atoi(args[arg+1]);
1333 arg += 2;
1334 }
1335 else if (!strcmp(args[arg], "depth")) {
1336 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001337 memprintf(err, "%s : '%s' expects a positive integer (got '%s').", args[0], args[arg], args[arg+1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001338 return -1;
1339 }
1340 /* hint: we store the position of the ending '/' (depth+1) so
1341 * that we avoid a comparison while computing the hash.
1342 */
1343 curproxy->uri_dirs_depth1 = atoi(args[arg+1]) + 1;
1344 arg += 2;
1345 }
Oskar Stolc8dc41842012-05-19 10:19:54 +01001346 else if (!strcmp(args[arg], "whole")) {
1347 curproxy->uri_whole = 1;
1348 arg += 1;
1349 }
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001350 else {
Oskar Stolc8dc41842012-05-19 10:19:54 +01001351 memprintf(err, "%s only accepts parameters 'len', 'depth', and 'whole' (got '%s').", args[0], args[arg]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001352 return -1;
1353 }
1354 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001355 }
Willy Tarreau01732802007-11-01 22:48:15 +01001356 else if (!strcmp(args[0], "url_param")) {
1357 if (!*args[1]) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001358 memprintf(err, "%s requires an URL parameter name.", args[0]);
Willy Tarreau01732802007-11-01 22:48:15 +01001359 return -1;
1360 }
Willy Tarreau31682232007-11-29 15:38:04 +01001361 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1362 curproxy->lbprm.algo |= BE_LB_ALGO_PH;
Willy Tarreaua534fea2008-08-03 12:19:50 +02001363
1364 free(curproxy->url_param_name);
Willy Tarreau01732802007-11-01 22:48:15 +01001365 curproxy->url_param_name = strdup(args[1]);
Willy Tarreaua534fea2008-08-03 12:19:50 +02001366 curproxy->url_param_len = strlen(args[1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001367 if (*args[2]) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001368 if (strcmp(args[2], "check_post")) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001369 memprintf(err, "%s only accepts 'check_post' modifier (got '%s').", args[0], args[2]);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001370 return -1;
1371 }
1372 if (*args[3]) {
1373 /* TODO: maybe issue a warning if there is no value, no digits or too long */
1374 curproxy->url_param_post_limit = str2ui(args[3]);
1375 }
1376 /* if no limit, or faul value in args[3], then default to a moderate wordlen */
1377 if (!curproxy->url_param_post_limit)
1378 curproxy->url_param_post_limit = 48;
1379 else if ( curproxy->url_param_post_limit < 3 )
1380 curproxy->url_param_post_limit = 3; /* minimum example: S=3 or \r\nS=6& */
1381 }
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 */