blob: baf02c2ede6aae72d50042c0b763e30a9a7a8ffd [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 Tarreau4aac7db2014-05-16 11:48:10 +020042#include <proto/log.h>
Willy Tarreau3fdb3662012-11-12 00:42:33 +010043#include <proto/obj_type.h>
Willy Tarreaud4c33c82013-01-07 21:59:07 +010044#include <proto/payload.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020045#include <proto/protocol.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020046#include <proto/proto_http.h>
Willy Tarreaua8f55d52010-05-31 17:44:19 +020047#include <proto/proto_tcp.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020048#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020049#include <proto/queue.h>
Willy Tarreaud4c33c82013-01-07 21:59:07 +010050#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010051#include <proto/server.h>
Willy Tarreau7c669d72008-06-20 15:04:11 +020052#include <proto/session.h>
Willy Tarreau75bf2c92012-08-20 17:01:35 +020053#include <proto/raw_sock.h>
Willy Tarreau9e000c62011-03-10 14:03:36 +010054#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020055#include <proto/task.h>
56
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050057int be_lastsession(const struct proxy *be)
58{
59 if (be->be_counters.last_sess)
60 return now.tv_sec - be->be_counters.last_sess;
61
62 return -1;
63}
64
Bhaskar98634f02013-10-29 23:30:51 -040065/* helper function to invoke the correct hash method */
Dan Dubovik82456752014-07-08 08:51:03 -070066static unsigned int gen_hash(const struct proxy* px, const char* key, unsigned long len)
Bhaskar98634f02013-10-29 23:30:51 -040067{
Dan Dubovik82456752014-07-08 08:51:03 -070068 unsigned int hash;
Bhaskar98634f02013-10-29 23:30:51 -040069
70 switch (px->lbprm.algo & BE_LB_HASH_FUNC) {
71 case BE_LB_HFCN_DJB2:
72 hash = hash_djb2(key, len);
73 break;
Willy Tarreaua0f42712013-11-14 14:30:35 +010074 case BE_LB_HFCN_WT6:
75 hash = hash_wt6(key, len);
76 break;
Bhaskar98634f02013-10-29 23:30:51 -040077 case BE_LB_HFCN_SDBM:
78 /* this is the default hash function */
79 default:
80 hash = hash_sdbm(key, len);
81 break;
82 }
83
84 return hash;
85}
86
Willy Tarreaubaaee002006-06-26 02:48:02 +020087/*
88 * This function recounts the number of usable active and backup servers for
89 * proxy <p>. These numbers are returned into the p->srv_act and p->srv_bck.
Willy Tarreaub625a082007-11-26 01:15:43 +010090 * This function also recomputes the total active and backup weights. However,
Willy Tarreauf4cca452008-03-08 21:42:54 +010091 * it does not update tot_weight nor tot_used. Use update_backend_weight() for
Willy Tarreaub625a082007-11-26 01:15:43 +010092 * this.
Willy Tarreaubaaee002006-06-26 02:48:02 +020093 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +020094void recount_servers(struct proxy *px)
Willy Tarreaubaaee002006-06-26 02:48:02 +020095{
96 struct server *srv;
97
Willy Tarreau20697042007-11-15 23:26:18 +010098 px->srv_act = px->srv_bck = 0;
99 px->lbprm.tot_wact = px->lbprm.tot_wbck = 0;
Willy Tarreaub625a082007-11-26 01:15:43 +0100100 px->lbprm.fbck = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200101 for (srv = px->srv; srv != NULL; srv = srv->next) {
Willy Tarreau87eb1d62014-05-13 18:51:40 +0200102 if (!srv_is_usable(srv))
Willy Tarreaub625a082007-11-26 01:15:43 +0100103 continue;
104
Willy Tarreauc93cd162014-05-13 15:54:22 +0200105 if (srv->flags & SRV_F_BACKUP) {
Willy Tarreaub625a082007-11-26 01:15:43 +0100106 if (!px->srv_bck &&
Willy Tarreauf4cca452008-03-08 21:42:54 +0100107 !(px->options & PR_O_USE_ALL_BK))
Willy Tarreaub625a082007-11-26 01:15:43 +0100108 px->lbprm.fbck = srv;
109 px->srv_bck++;
110 px->lbprm.tot_wbck += srv->eweight;
111 } else {
112 px->srv_act++;
113 px->lbprm.tot_wact += srv->eweight;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200114 }
115 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100116}
Willy Tarreau20697042007-11-15 23:26:18 +0100117
Willy Tarreaub625a082007-11-26 01:15:43 +0100118/* This function simply updates the backend's tot_weight and tot_used values
119 * after servers weights have been updated. It is designed to be used after
120 * recount_servers() or equivalent.
121 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +0200122void update_backend_weight(struct proxy *px)
Willy Tarreaub625a082007-11-26 01:15:43 +0100123{
Willy Tarreau20697042007-11-15 23:26:18 +0100124 if (px->srv_act) {
125 px->lbprm.tot_weight = px->lbprm.tot_wact;
126 px->lbprm.tot_used = px->srv_act;
127 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100128 else if (px->lbprm.fbck) {
129 /* use only the first backup server */
130 px->lbprm.tot_weight = px->lbprm.fbck->eweight;
131 px->lbprm.tot_used = 1;
Willy Tarreau20697042007-11-15 23:26:18 +0100132 }
133 else {
Willy Tarreaub625a082007-11-26 01:15:43 +0100134 px->lbprm.tot_weight = px->lbprm.tot_wbck;
135 px->lbprm.tot_used = px->srv_bck;
Willy Tarreau20697042007-11-15 23:26:18 +0100136 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100137}
138
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200139/*
140 * This function tries to find a running server for the proxy <px> following
141 * the source hash method. Depending on the number of active/backup servers,
142 * it will either look for active servers, or for backup servers.
143 * If any server is found, it will be returned. If no valid server is found,
144 * NULL is returned.
145 */
146struct server *get_server_sh(struct proxy *px, const char *addr, int len)
147{
148 unsigned int h, l;
149
150 if (px->lbprm.tot_weight == 0)
151 return NULL;
152
153 l = h = 0;
154
155 /* note: we won't hash if there's only one server left */
156 if (px->lbprm.tot_used == 1)
157 goto hash_done;
158
159 while ((l + sizeof (int)) <= len) {
160 h ^= ntohl(*(unsigned int *)(&addr[l]));
161 l += sizeof (int);
162 }
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500163 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100164 h = full_hash(h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200165 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200166 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
167 return chash_get_server_hash(px, h);
168 else
169 return map_get_server_hash(px, h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200170}
171
172/*
173 * This function tries to find a running server for the proxy <px> following
174 * the URI hash method. In order to optimize cache hits, the hash computation
175 * ends at the question mark. Depending on the number of active/backup servers,
176 * it will either look for active servers, or for backup servers.
177 * If any server is found, it will be returned. If no valid server is found,
178 * NULL is returned.
179 *
180 * This code was contributed by Guillaume Dallaire, who also selected this hash
181 * algorithm out of a tens because it gave him the best results.
182 *
183 */
184struct server *get_server_uh(struct proxy *px, char *uri, int uri_len)
185{
Dan Dubovik82456752014-07-08 08:51:03 -0700186 unsigned int hash = 0;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200187 int c;
188 int slashes = 0;
Bhaskar98634f02013-10-29 23:30:51 -0400189 const char *start, *end;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200190
191 if (px->lbprm.tot_weight == 0)
192 return NULL;
193
194 /* note: we won't hash if there's only one server left */
195 if (px->lbprm.tot_used == 1)
196 goto hash_done;
197
198 if (px->uri_len_limit)
199 uri_len = MIN(uri_len, px->uri_len_limit);
200
Bhaskar98634f02013-10-29 23:30:51 -0400201 start = end = uri;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200202 while (uri_len--) {
Willy Tarreauac032922014-10-17 12:11:50 +0200203 c = *end;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200204 if (c == '/') {
205 slashes++;
206 if (slashes == px->uri_dirs_depth1) /* depth+1 */
207 break;
208 }
Oskar Stolc8dc41842012-05-19 10:19:54 +0100209 else if (c == '?' && !px->uri_whole)
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200210 break;
Willy Tarreauac032922014-10-17 12:11:50 +0200211 end++;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200212 }
Bhaskar98634f02013-10-29 23:30:51 -0400213
214 hash = gen_hash(px, start, (end - start));
215
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500216 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100217 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200218 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200219 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
220 return chash_get_server_hash(px, hash);
221 else
222 return map_get_server_hash(px, hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200223}
224
Willy Tarreau01732802007-11-01 22:48:15 +0100225/*
226 * This function tries to find a running server for the proxy <px> following
227 * the URL parameter hash method. It looks for a specific parameter in the
228 * URL and hashes it to compute the server ID. This is useful to optimize
229 * performance by avoiding bounces between servers in contexts where sessions
230 * are shared but cookies are not usable. If the parameter is not found, NULL
231 * is returned. If any server is found, it will be returned. If no valid server
232 * is found, NULL is returned.
Willy Tarreau01732802007-11-01 22:48:15 +0100233 */
234struct server *get_server_ph(struct proxy *px, const char *uri, int uri_len)
235{
Dan Dubovik82456752014-07-08 08:51:03 -0700236 unsigned int hash = 0;
Bhaskar98634f02013-10-29 23:30:51 -0400237 const char *start, *end;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200238 const char *p;
239 const char *params;
Willy Tarreau01732802007-11-01 22:48:15 +0100240 int plen;
241
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200242 /* when tot_weight is 0 then so is srv_count */
Willy Tarreau20697042007-11-15 23:26:18 +0100243 if (px->lbprm.tot_weight == 0)
Willy Tarreau01732802007-11-01 22:48:15 +0100244 return NULL;
245
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200246 if ((p = memchr(uri, '?', uri_len)) == NULL)
247 return NULL;
248
Willy Tarreau01732802007-11-01 22:48:15 +0100249 p++;
250
251 uri_len -= (p - uri);
252 plen = px->url_param_len;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200253 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100254
255 while (uri_len > plen) {
256 /* Look for the parameter name followed by an equal symbol */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200257 if (params[plen] == '=') {
258 if (memcmp(params, px->url_param_name, plen) == 0) {
259 /* OK, we have the parameter here at <params>, and
Willy Tarreau01732802007-11-01 22:48:15 +0100260 * the value after the equal sign, at <p>
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200261 * skip the equal symbol
Willy Tarreau01732802007-11-01 22:48:15 +0100262 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200263 p += plen + 1;
Bhaskar98634f02013-10-29 23:30:51 -0400264 start = end = p;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200265 uri_len -= plen + 1;
266
Bhaskar98634f02013-10-29 23:30:51 -0400267 while (uri_len && *end != '&') {
Willy Tarreau01732802007-11-01 22:48:15 +0100268 uri_len--;
Bhaskar98634f02013-10-29 23:30:51 -0400269 end++;
Willy Tarreau01732802007-11-01 22:48:15 +0100270 }
Bhaskar98634f02013-10-29 23:30:51 -0400271 hash = gen_hash(px, start, (end - start));
272
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500273 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100274 hash = full_hash(hash);
Bhaskar98634f02013-10-29 23:30:51 -0400275
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200276 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
277 return chash_get_server_hash(px, hash);
278 else
279 return map_get_server_hash(px, hash);
Willy Tarreau01732802007-11-01 22:48:15 +0100280 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200281 }
282 /* skip to next parameter */
283 p = memchr(params, '&', uri_len);
284 if (!p)
285 return NULL;
286 p++;
287 uri_len -= (p - params);
288 params = p;
289 }
290 return NULL;
291}
292
293/*
294 * this does the same as the previous server_ph, but check the body contents
295 */
296struct server *get_server_ph_post(struct session *s)
297{
Dan Dubovik82456752014-07-08 08:51:03 -0700298 unsigned int hash = 0;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200299 struct http_txn *txn = &s->txn;
Willy Tarreau7421efb2012-07-02 15:11:27 +0200300 struct channel *req = s->req;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200301 struct http_msg *msg = &txn->req;
302 struct proxy *px = s->be;
303 unsigned int plen = px->url_param_len;
Willy Tarreau2d8e4852014-04-17 20:08:17 +0200304 unsigned long len = http_body_bytes(msg);
Willy Tarreau0d090502014-04-17 20:31:44 +0200305 const char *params = b_ptr(req->buf, -http_data_rewind(msg));
Willy Tarreau157dd632009-12-06 19:18:09 +0100306 const char *p = params;
Bhaskar98634f02013-10-29 23:30:51 -0400307 const char *start, *end;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200308
Willy Tarreau157dd632009-12-06 19:18:09 +0100309 if (len == 0)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200310 return NULL;
311
Willy Tarreau522aab32015-05-02 00:05:47 +0200312 if (len > req->buf->data + req->buf->size - p)
313 len = req->buf->data + req->buf->size - p;
314
Willy Tarreau157dd632009-12-06 19:18:09 +0100315 if (px->lbprm.tot_weight == 0)
316 return NULL;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200317
318 while (len > plen) {
319 /* Look for the parameter name followed by an equal symbol */
320 if (params[plen] == '=') {
321 if (memcmp(params, px->url_param_name, plen) == 0) {
322 /* OK, we have the parameter here at <params>, and
323 * the value after the equal sign, at <p>
324 * skip the equal symbol
325 */
326 p += plen + 1;
Bhaskar98634f02013-10-29 23:30:51 -0400327 start = end = p;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200328 len -= plen + 1;
329
Bhaskar98634f02013-10-29 23:30:51 -0400330 while (len && *end != '&') {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200331 if (unlikely(!HTTP_IS_TOKEN(*p))) {
Willy Tarreau157dd632009-12-06 19:18:09 +0100332 /* if in a POST, body must be URI encoded or it's not a URI.
Bhaskar98634f02013-10-29 23:30:51 -0400333 * Do not interpret any possible binary data as a parameter.
Willy Tarreau157dd632009-12-06 19:18:09 +0100334 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200335 if (likely(HTTP_IS_LWS(*p))) /* eol, uncertain uri len */
336 break;
337 return NULL; /* oh, no; this is not uri-encoded.
338 * This body does not contain parameters.
339 */
340 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200341 len--;
Bhaskar98634f02013-10-29 23:30:51 -0400342 end++;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200343 /* should we break if vlen exceeds limit? */
344 }
Bhaskar98634f02013-10-29 23:30:51 -0400345 hash = gen_hash(px, start, (end - start));
346
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500347 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100348 hash = full_hash(hash);
Bhaskar98634f02013-10-29 23:30:51 -0400349
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200350 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
351 return chash_get_server_hash(px, hash);
352 else
353 return map_get_server_hash(px, hash);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200354 }
355 }
Willy Tarreau01732802007-11-01 22:48:15 +0100356 /* skip to next parameter */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200357 p = memchr(params, '&', len);
Willy Tarreau01732802007-11-01 22:48:15 +0100358 if (!p)
359 return NULL;
360 p++;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200361 len -= (p - params);
362 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100363 }
364 return NULL;
365}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200366
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200367
Willy Tarreaubaaee002006-06-26 02:48:02 +0200368/*
Benoitaffb4812009-03-25 13:02:10 +0100369 * This function tries to find a running server for the proxy <px> following
370 * the Header parameter hash method. It looks for a specific parameter in the
371 * URL and hashes it to compute the server ID. This is useful to optimize
372 * performance by avoiding bounces between servers in contexts where sessions
373 * are shared but cookies are not usable. If the parameter is not found, NULL
374 * is returned. If any server is found, it will be returned. If no valid server
375 * is found, NULL is returned.
376 */
377struct server *get_server_hh(struct session *s)
378{
Dan Dubovik82456752014-07-08 08:51:03 -0700379 unsigned int hash = 0;
Benoitaffb4812009-03-25 13:02:10 +0100380 struct http_txn *txn = &s->txn;
Benoitaffb4812009-03-25 13:02:10 +0100381 struct proxy *px = s->be;
382 unsigned int plen = px->hh_len;
383 unsigned long len;
384 struct hdr_ctx ctx;
385 const char *p;
Bhaskar98634f02013-10-29 23:30:51 -0400386 const char *start, *end;
Benoitaffb4812009-03-25 13:02:10 +0100387
388 /* tot_weight appears to mean srv_count */
389 if (px->lbprm.tot_weight == 0)
390 return NULL;
391
Benoitaffb4812009-03-25 13:02:10 +0100392 ctx.idx = 0;
393
394 /* if the message is chunked, we skip the chunk size, but use the value as len */
Willy Tarreau211cdec2014-04-17 20:18:08 +0200395 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 +0100396
397 /* if the header is not found or empty, let's fallback to round robin */
398 if (!ctx.idx || !ctx.vlen)
399 return NULL;
400
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200401 /* note: we won't hash if there's only one server left */
402 if (px->lbprm.tot_used == 1)
403 goto hash_done;
404
Benoitaffb4812009-03-25 13:02:10 +0100405 /* Found a the hh_name in the headers.
406 * we will compute the hash based on this value ctx.val.
407 */
408 len = ctx.vlen;
409 p = (char *)ctx.line + ctx.val;
410 if (!px->hh_match_domain) {
Bhaskar98634f02013-10-29 23:30:51 -0400411 hash = gen_hash(px, p, len);
Benoitaffb4812009-03-25 13:02:10 +0100412 } else {
413 int dohash = 0;
Cyril Bonté7ccea262015-01-04 15:17:36 +0100414 p += len;
Benoitaffb4812009-03-25 13:02:10 +0100415 /* special computation, use only main domain name, not tld/host
416 * going back from the end of string, start hashing at first
417 * dot stop at next.
418 * This is designed to work with the 'Host' header, and requires
419 * a special option to activate this.
420 */
Cyril Bonté7ccea262015-01-04 15:17:36 +0100421 end = p;
Benoitaffb4812009-03-25 13:02:10 +0100422 while (len) {
Cyril Bonté7ccea262015-01-04 15:17:36 +0100423 if (dohash) {
424 /* Rewind the pointer until the previous char
425 * is a dot, this will allow to set the start
426 * position of the domain. */
427 if (*(p - 1) == '.')
Benoitaffb4812009-03-25 13:02:10 +0100428 break;
Benoitaffb4812009-03-25 13:02:10 +0100429 }
Cyril Bonté7ccea262015-01-04 15:17:36 +0100430 else if (*p == '.') {
431 /* The pointer is rewinded to the dot before the
432 * tld, we memorize the end of the domain and
433 * can enter the domain processing. */
434 end = p;
435 dohash = 1;
436 }
Benoitaffb4812009-03-25 13:02:10 +0100437 p--;
Cyril Bonté7ccea262015-01-04 15:17:36 +0100438 len--;
Benoitaffb4812009-03-25 13:02:10 +0100439 }
Cyril Bonté7ccea262015-01-04 15:17:36 +0100440 start = p;
Bhaskar98634f02013-10-29 23:30:51 -0400441 hash = gen_hash(px, start, (end - start));
Benoitaffb4812009-03-25 13:02:10 +0100442 }
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500443 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100444 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200445 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200446 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
447 return chash_get_server_hash(px, hash);
448 else
449 return map_get_server_hash(px, hash);
Benoitaffb4812009-03-25 13:02:10 +0100450}
451
Willy Tarreau34db1082012-04-19 17:16:54 +0200452/* RDP Cookie HASH. */
Emeric Brun736aa232009-06-30 17:56:00 +0200453struct server *get_server_rch(struct session *s)
454{
Dan Dubovik82456752014-07-08 08:51:03 -0700455 unsigned int hash = 0;
Emeric Brun736aa232009-06-30 17:56:00 +0200456 struct proxy *px = s->be;
457 unsigned long len;
Emeric Brun736aa232009-06-30 17:56:00 +0200458 int ret;
Willy Tarreau37406352012-04-23 16:16:37 +0200459 struct sample smp;
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200460 int rewind;
Emeric Brun736aa232009-06-30 17:56:00 +0200461
462 /* tot_weight appears to mean srv_count */
463 if (px->lbprm.tot_weight == 0)
464 return NULL;
465
Willy Tarreau37406352012-04-23 16:16:37 +0200466 memset(&smp, 0, sizeof(smp));
Emeric Brun736aa232009-06-30 17:56:00 +0200467
Willy Tarreau9b28e032012-10-12 23:49:43 +0200468 b_rew(s->req->buf, rewind = s->req->buf->o);
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200469
Willy Tarreaucadd8c92013-07-22 18:09:52 +0200470 ret = fetch_rdp_cookie_name(s, &smp, px->hh_name, px->hh_len);
Willy Tarreauf853c462012-04-23 18:53:56 +0200471 len = smp.data.str.len;
Willy Tarreau664092c2011-12-16 19:11:42 +0100472
Willy Tarreau9b28e032012-10-12 23:49:43 +0200473 b_adv(s->req->buf, rewind);
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200474
Willy Tarreau37406352012-04-23 16:16:37 +0200475 if (ret == 0 || (smp.flags & SMP_F_MAY_CHANGE) || len == 0)
Emeric Brun736aa232009-06-30 17:56:00 +0200476 return NULL;
477
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200478 /* note: we won't hash if there's only one server left */
479 if (px->lbprm.tot_used == 1)
480 goto hash_done;
481
Emeric Brun736aa232009-06-30 17:56:00 +0200482 /* Found a the hh_name in the headers.
483 * we will compute the hash based on this value ctx.val.
484 */
Bhaskar98634f02013-10-29 23:30:51 -0400485 hash = gen_hash(px, smp.data.str.str, len);
486
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500487 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100488 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200489 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200490 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
491 return chash_get_server_hash(px, hash);
492 else
493 return map_get_server_hash(px, hash);
Emeric Brun736aa232009-06-30 17:56:00 +0200494}
Benoitaffb4812009-03-25 13:02:10 +0100495
496/*
Willy Tarreau7c669d72008-06-20 15:04:11 +0200497 * This function applies the load-balancing algorithm to the session, as
498 * defined by the backend it is assigned to. The session is then marked as
499 * 'assigned'.
500 *
501 * This function MAY NOT be called with SN_ASSIGNED already set. If the session
502 * had a server previously assigned, it is rebalanced, trying to avoid the same
Willy Tarreau827aee92011-03-10 16:55:02 +0100503 * server, which should still be present in target_srv(&s->target) before the call.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200504 * The function tries to keep the original connection slot if it reconnects to
505 * the same server, otherwise it releases it and tries to offer it.
506 *
507 * It is illegal to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200508 *
509 * It may return :
Willy Tarreau664beb82011-03-10 11:38:29 +0100510 * SRV_STATUS_OK if everything is OK. ->srv and ->target are assigned.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200511 * SRV_STATUS_NOSRV if no server is available. Session is not ASSIGNED
512 * SRV_STATUS_FULL if all servers are saturated. Session is not ASSIGNED
Willy Tarreaubaaee002006-06-26 02:48:02 +0200513 * SRV_STATUS_INTERNAL for other unrecoverable errors.
514 *
Willy Tarreau7c669d72008-06-20 15:04:11 +0200515 * Upon successful return, the session flag SN_ASSIGNED is set to indicate that
Willy Tarreau827aee92011-03-10 16:55:02 +0100516 * it does not need to be called anymore. This means that target_srv(&s->target)
517 * can be trusted in balance and direct modes.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200518 *
519 */
520
521int assign_server(struct session *s)
522{
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200523 struct connection *conn;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200524 struct server *conn_slot;
Willy Tarreau827aee92011-03-10 16:55:02 +0100525 struct server *srv, *prev_srv;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200526 int err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100527
Simon Horman8effd3d2011-08-13 08:03:52 +0900528 DPRINTF(stderr,"assign_server : s=%p\n",s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200529
Willy Tarreau7c669d72008-06-20 15:04:11 +0200530 err = SRV_STATUS_INTERNAL;
531 if (unlikely(s->pend_pos || s->flags & SN_ASSIGNED))
532 goto out_err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100533
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100534 prev_srv = objt_server(s->target);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200535 conn_slot = s->srv_conn;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200536
Willy Tarreau7c669d72008-06-20 15:04:11 +0200537 /* We have to release any connection slot before applying any LB algo,
538 * otherwise we may erroneously end up with no available slot.
539 */
540 if (conn_slot)
541 sess_change_server(s, NULL);
542
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100543 /* We will now try to find the good server and store it into <objt_server(s->target)>.
544 * Note that <objt_server(s->target)> may be NULL in case of dispatch or proxy mode,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200545 * as well as if no server is available (check error code).
546 */
Willy Tarreau1a20a5d2007-11-01 21:08:19 +0100547
Willy Tarreau827aee92011-03-10 16:55:02 +0100548 srv = NULL;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100549 s->target = NULL;
Willy Tarreau9420b122013-12-15 18:58:25 +0100550 conn = objt_conn(s->req->cons->end);
Willy Tarreau664beb82011-03-10 11:38:29 +0100551
Willy Tarreau068621e2013-12-23 15:11:25 +0100552 if (conn &&
Willy Tarreau2481d162014-02-19 18:40:43 +0100553 (conn->flags & CO_FL_CONNECTED) &&
Willy Tarreau9420b122013-12-15 18:58:25 +0100554 objt_server(conn->target) && __objt_server(conn->target)->proxy == s->be &&
Willy Tarreauc35362a2014-04-25 13:58:37 +0200555 ((s->txn.flags & TX_PREFER_LAST) ||
556 ((s->be->options & PR_O_PREF_LAST) &&
557 (!s->be->max_ka_queue ||
558 server_has_room(__objt_server(conn->target)) ||
559 (__objt_server(conn->target)->nbpend + 1) < s->be->max_ka_queue))) &&
Willy Tarreau87eb1d62014-05-13 18:51:40 +0200560 srv_is_usable(__objt_server(conn->target))) {
Willy Tarreau9420b122013-12-15 18:58:25 +0100561 /* This session was relying on a server in a previous request
Godbachcfbb93f2014-12-10 10:21:30 +0800562 * and the proxy has "option prefer-last-server" set, so
Willy Tarreau9420b122013-12-15 18:58:25 +0100563 * let's try to reuse the same server.
564 */
565 srv = __objt_server(conn->target);
566 s->target = &srv->obj_type;
567 }
568 else if (s->be->lbprm.algo & BE_LB_KIND) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200569 /* we must check if we have at least one server available */
570 if (!s->be->lbprm.tot_weight) {
571 err = SRV_STATUS_NOSRV;
572 goto out;
573 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200574
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200575 /* First check whether we need to fetch some data or simply call
576 * the LB lookup function. Only the hashing functions will need
577 * some input data in fact, and will support multiple algorithms.
578 */
579 switch (s->be->lbprm.algo & BE_LB_LKUP) {
580 case BE_LB_LKUP_RRTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100581 srv = fwrr_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200582 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200583
Willy Tarreauf09c6602012-02-13 17:12:08 +0100584 case BE_LB_LKUP_FSTREE:
585 srv = fas_get_next_server(s->be, prev_srv);
586 break;
587
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200588 case BE_LB_LKUP_LCTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100589 srv = fwlc_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200590 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200591
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200592 case BE_LB_LKUP_CHTREE:
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200593 case BE_LB_LKUP_MAP:
Willy Tarreau9757a382009-10-03 12:56:50 +0200594 if ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200595 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100596 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200597 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100598 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau9757a382009-10-03 12:56:50 +0200599 break;
600 }
601 else if ((s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_HI) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200602 /* unknown balancing algorithm */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200603 err = SRV_STATUS_INTERNAL;
604 goto out;
605 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200606
607 switch (s->be->lbprm.algo & BE_LB_PARM) {
608 case BE_LB_HASH_SRC:
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200609 conn = objt_conn(s->req->prod->end);
610 if (conn && conn->addr.from.ss_family == AF_INET) {
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200611 srv = get_server_sh(s->be,
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200612 (void *)&((struct sockaddr_in *)&conn->addr.from)->sin_addr,
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200613 4);
614 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200615 else if (conn && conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200616 srv = get_server_sh(s->be,
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200617 (void *)&((struct sockaddr_in6 *)&conn->addr.from)->sin6_addr,
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200618 16);
619 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200620 else {
621 /* unknown IP family */
622 err = SRV_STATUS_INTERNAL;
623 goto out;
624 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200625 break;
626
627 case BE_LB_HASH_URI:
628 /* URI hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100629 if (s->txn.req.msg_state < HTTP_MSG_BODY)
630 break;
Willy Tarreau827aee92011-03-10 16:55:02 +0100631 srv = get_server_uh(s->be,
Willy Tarreauda6eed62014-04-17 20:24:24 +0200632 b_ptr(s->req->buf, -http_uri_rewind(&s->txn.req)),
Willy Tarreau827aee92011-03-10 16:55:02 +0100633 s->txn.req.sl.rq.u_l);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200634 break;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200635
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200636 case BE_LB_HASH_PRM:
637 /* URL Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100638 if (s->txn.req.msg_state < HTTP_MSG_BODY)
639 break;
Willy Tarreau61a21a32011-03-01 20:35:49 +0100640
Willy Tarreau827aee92011-03-10 16:55:02 +0100641 srv = get_server_ph(s->be,
Willy Tarreauda6eed62014-04-17 20:24:24 +0200642 b_ptr(s->req->buf, -http_uri_rewind(&s->txn.req)),
Willy Tarreau827aee92011-03-10 16:55:02 +0100643 s->txn.req.sl.rq.u_l);
Willy Tarreau61a21a32011-03-01 20:35:49 +0100644
Willy Tarreau827aee92011-03-10 16:55:02 +0100645 if (!srv && s->txn.meth == HTTP_METH_POST)
646 srv = get_server_ph_post(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200647 break;
Benoitaffb4812009-03-25 13:02:10 +0100648
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200649 case BE_LB_HASH_HDR:
650 /* Header Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100651 if (s->txn.req.msg_state < HTTP_MSG_BODY)
652 break;
Willy Tarreau827aee92011-03-10 16:55:02 +0100653 srv = get_server_hh(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200654 break;
655
656 case BE_LB_HASH_RDP:
657 /* RDP Cookie hashing */
Willy Tarreau827aee92011-03-10 16:55:02 +0100658 srv = get_server_rch(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200659 break;
660
661 default:
662 /* unknown balancing algorithm */
663 err = SRV_STATUS_INTERNAL;
664 goto out;
Benoitaffb4812009-03-25 13:02:10 +0100665 }
Emeric Brun736aa232009-06-30 17:56:00 +0200666
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200667 /* If the hashing parameter was not found, let's fall
668 * back to round robin on the map.
669 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100670 if (!srv) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200671 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100672 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200673 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100674 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200675 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200676
677 /* end of map-based LB */
Emeric Brun736aa232009-06-30 17:56:00 +0200678 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200679
Willy Tarreau7c669d72008-06-20 15:04:11 +0200680 default:
681 /* unknown balancing algorithm */
682 err = SRV_STATUS_INTERNAL;
683 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200684 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200685
Willy Tarreau827aee92011-03-10 16:55:02 +0100686 if (!srv) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200687 err = SRV_STATUS_FULL;
688 goto out;
689 }
Willy Tarreau827aee92011-03-10 16:55:02 +0100690 else if (srv != prev_srv) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100691 s->be->be_counters.cum_lbconn++;
Willy Tarreau827aee92011-03-10 16:55:02 +0100692 srv->counters.cum_lbconn++;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100693 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100694 s->target = &srv->obj_type;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200695 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200696 else if (s->be->options & (PR_O_DISPATCH | PR_O_TRANSP)) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100697 s->target = &s->be->obj_type;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200698 }
David du Colombier6f5ccb12011-03-10 22:26:24 +0100699 else if ((s->be->options & PR_O_HTTP_PROXY) &&
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200700 (conn = objt_conn(s->req->cons->end)) &&
701 is_addr(&conn->addr.to)) {
Willy Tarreau664beb82011-03-10 11:38:29 +0100702 /* in proxy mode, we need a valid destination address */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100703 s->target = &s->be->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +0100704 }
705 else {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200706 err = SRV_STATUS_NOSRV;
707 goto out;
708 }
709
710 s->flags |= SN_ASSIGNED;
711 err = SRV_STATUS_OK;
712 out:
713
714 /* Either we take back our connection slot, or we offer it to someone
715 * else if we don't need it anymore.
716 */
717 if (conn_slot) {
Willy Tarreau827aee92011-03-10 16:55:02 +0100718 if (conn_slot == srv) {
719 sess_change_server(s, srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200720 } else {
721 if (may_dequeue_tasks(conn_slot, s->be))
722 process_srv_queue(conn_slot);
723 }
724 }
725
726 out_err:
727 return err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200728}
729
730
731/*
732 * This function assigns a server address to a session, and sets SN_ADDR_SET.
733 * The address is taken from the currently assigned server, or from the
734 * dispatch or transparent address.
735 *
736 * It may return :
737 * SRV_STATUS_OK if everything is OK.
738 * SRV_STATUS_INTERNAL for other unrecoverable errors.
739 *
740 * Upon successful return, the session flag SN_ADDR_SET is set. This flag is
741 * not cleared, so it's to the caller to clear it if required.
742 *
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200743 * The caller is responsible for having already assigned a connection
744 * to si->end.
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200745 *
Willy Tarreaubaaee002006-06-26 02:48:02 +0200746 */
747int assign_server_address(struct session *s)
748{
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200749 struct connection *cli_conn = objt_conn(s->req->prod->end);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200750 struct connection *srv_conn = objt_conn(s->req->cons->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200751
Willy Tarreaubaaee002006-06-26 02:48:02 +0200752#ifdef DEBUG_FULL
753 fprintf(stderr,"assign_server_address : s=%p\n",s);
754#endif
755
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200756 if ((s->flags & SN_DIRECT) || (s->be->lbprm.algo & BE_LB_KIND)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200757 /* A server is necessarily known for this session */
758 if (!(s->flags & SN_ASSIGNED))
759 return SRV_STATUS_INTERNAL;
760
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200761 srv_conn->addr.to = objt_server(s->target)->addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200762
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200763 if (!is_addr(&srv_conn->addr.to) && cli_conn) {
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200764 /* if the server has no address, we use the same address
765 * the client asked, which is handy for remapping ports
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200766 * locally on multiple addresses at once. Nothing is done
767 * for AF_UNIX addresses.
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200768 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200769 conn_get_to_addr(cli_conn);
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200770
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200771 if (cli_conn->addr.to.ss_family == AF_INET) {
772 ((struct sockaddr_in *)&srv_conn->addr.to)->sin_addr = ((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
773 } else if (cli_conn->addr.to.ss_family == AF_INET6) {
774 ((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 +0200775 }
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200776 }
777
Willy Tarreaubaaee002006-06-26 02:48:02 +0200778 /* if this server remaps proxied ports, we'll use
779 * the port the client connected to with an offset. */
Willy Tarreauc93cd162014-05-13 15:54:22 +0200780 if ((objt_server(s->target)->flags & SRV_F_MAPPORTS) && cli_conn) {
David du Colombier6f5ccb12011-03-10 22:26:24 +0100781 int base_port;
782
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200783 conn_get_to_addr(cli_conn);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100784
785 /* First, retrieve the port from the incoming connection */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200786 base_port = get_host_port(&cli_conn->addr.to);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100787
788 /* Second, assign the outgoing connection's port */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200789 base_port += get_host_port(&srv_conn->addr.to);
790 set_host_port(&srv_conn->addr.to, base_port);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200791 }
792 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200793 else if (s->be->options & PR_O_DISPATCH) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200794 /* connect to the defined dispatch addr */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200795 srv_conn->addr.to = s->be->dispatch_addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200796 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200797 else if ((s->be->options & PR_O_TRANSP) && cli_conn) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200798 /* in transparent mode, use the original dest addr if no dispatch specified */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200799 conn_get_to_addr(cli_conn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200800
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200801 if (cli_conn->addr.to.ss_family == AF_INET || cli_conn->addr.to.ss_family == AF_INET6)
802 srv_conn->addr.to = cli_conn->addr.to;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200803 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100804 else if (s->be->options & PR_O_HTTP_PROXY) {
805 /* If HTTP PROXY option is set, then server is already assigned
806 * during incoming client request parsing. */
807 }
Willy Tarreau1a1158b2007-01-20 11:07:46 +0100808 else {
809 /* no server and no LB algorithm ! */
810 return SRV_STATUS_INTERNAL;
811 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200812
813 s->flags |= SN_ADDR_SET;
814 return SRV_STATUS_OK;
815}
816
817
818/* This function assigns a server to session <s> if required, and can add the
819 * connection to either the assigned server's queue or to the proxy's queue.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200820 * If ->srv_conn is set, the session is first released from the server.
821 * It may also be called with SN_DIRECT and/or SN_ASSIGNED though. It will
822 * be called before any connection and after any retry or redispatch occurs.
823 *
824 * It is not allowed to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200825 *
826 * Returns :
827 *
828 * SRV_STATUS_OK if everything is OK.
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100829 * SRV_STATUS_NOSRV if no server is available. objt_server(s->target) = NULL.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200830 * SRV_STATUS_QUEUED if the connection has been queued.
831 * SRV_STATUS_FULL if the server(s) is/are saturated and the
Willy Tarreau827aee92011-03-10 16:55:02 +0100832 * connection could not be queued at the server's,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200833 * which may be NULL if we queue on the backend.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200834 * SRV_STATUS_INTERNAL for other unrecoverable errors.
835 *
836 */
837int assign_server_and_queue(struct session *s)
838{
839 struct pendconn *p;
Willy Tarreau827aee92011-03-10 16:55:02 +0100840 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200841 int err;
842
843 if (s->pend_pos)
844 return SRV_STATUS_INTERNAL;
845
Willy Tarreau7c669d72008-06-20 15:04:11 +0200846 err = SRV_STATUS_OK;
847 if (!(s->flags & SN_ASSIGNED)) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100848 struct server *prev_srv = objt_server(s->target);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100849
Willy Tarreau7c669d72008-06-20 15:04:11 +0200850 err = assign_server(s);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100851 if (prev_srv) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200852 /* This session was previously assigned to a server. We have to
853 * update the session's and the server's stats :
854 * - if the server changed :
855 * - set TX_CK_DOWN if txn.flags was TX_CK_VALID
856 * - set SN_REDISP if it was successfully redispatched
857 * - increment srv->redispatches and be->redispatches
858 * - if the server remained the same : update retries.
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100859 */
860
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100861 if (prev_srv != objt_server(s->target)) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200862 if ((s->txn.flags & TX_CK_MASK) == TX_CK_VALID) {
863 s->txn.flags &= ~TX_CK_MASK;
864 s->txn.flags |= TX_CK_DOWN;
865 }
866 s->flags |= SN_REDISP;
Willy Tarreau3d80d912011-03-10 11:42:13 +0100867 prev_srv->counters.redispatches++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100868 s->be->be_counters.redispatches++;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200869 } else {
Willy Tarreau3d80d912011-03-10 11:42:13 +0100870 prev_srv->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100871 s->be->be_counters.retries++;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100872 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100873 }
874 }
875
Willy Tarreaubaaee002006-06-26 02:48:02 +0200876 switch (err) {
877 case SRV_STATUS_OK:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200878 /* we have SN_ASSIGNED set */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100879 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100880 if (!srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200881 return SRV_STATUS_OK; /* dispatch or proxy mode */
882
883 /* If we already have a connection slot, no need to check any queue */
Willy Tarreau827aee92011-03-10 16:55:02 +0100884 if (s->srv_conn == srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200885 return SRV_STATUS_OK;
886
887 /* OK, this session already has an assigned server, but no
888 * connection slot yet. Either it is a redispatch, or it was
889 * assigned from persistence information (direct mode).
890 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100891 if ((s->flags & SN_REDIRECTABLE) && srv->rdr_len) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200892 /* server scheduled for redirection, and already assigned. We
893 * don't want to go further nor check the queue.
Willy Tarreau21d2af32008-02-14 20:25:24 +0100894 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100895 sess_change_server(s, srv); /* not really needed in fact */
Willy Tarreau21d2af32008-02-14 20:25:24 +0100896 return SRV_STATUS_OK;
897 }
898
Willy Tarreau7c669d72008-06-20 15:04:11 +0200899 /* We might have to queue this session if the assigned server is full.
900 * We know we have to queue it into the server's queue, so if a maxqueue
901 * is set on the server, we must also check that the server's queue is
902 * not full, in which case we have to return FULL.
903 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100904 if (srv->maxconn &&
905 (srv->nbpend || srv->served >= srv_dynamic_maxconn(srv))) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200906
Willy Tarreau827aee92011-03-10 16:55:02 +0100907 if (srv->maxqueue > 0 && srv->nbpend >= srv->maxqueue)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200908 return SRV_STATUS_FULL;
909
Willy Tarreaubaaee002006-06-26 02:48:02 +0200910 p = pendconn_add(s);
911 if (p)
912 return SRV_STATUS_QUEUED;
913 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200914 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200915 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200916
917 /* OK, we can use this server. Let's reserve our place */
Willy Tarreau827aee92011-03-10 16:55:02 +0100918 sess_change_server(s, srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200919 return SRV_STATUS_OK;
920
921 case SRV_STATUS_FULL:
922 /* queue this session into the proxy's queue */
923 p = pendconn_add(s);
924 if (p)
925 return SRV_STATUS_QUEUED;
926 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200927 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200928
929 case SRV_STATUS_NOSRV:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200930 return err;
931
Willy Tarreaubaaee002006-06-26 02:48:02 +0200932 case SRV_STATUS_INTERNAL:
933 return err;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200934
Willy Tarreaubaaee002006-06-26 02:48:02 +0200935 default:
936 return SRV_STATUS_INTERNAL;
937 }
Willy Tarreau5b6995c2008-01-13 16:31:17 +0100938}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200939
Willy Tarreaub1d67742010-03-29 19:36:59 +0200940/* If an explicit source binding is specified on the server and/or backend, and
941 * this source makes use of the transparent proxy, then it is extracted now and
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200942 * assigned to the session's pending connection. This function assumes that an
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200943 * outgoing connection has already been assigned to s->req->cons->end.
Willy Tarreaub1d67742010-03-29 19:36:59 +0200944 */
945static void assign_tproxy_address(struct session *s)
946{
Pieter Baauwd551fb52013-05-08 22:49:23 +0200947#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_TRANSPARENT)
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100948 struct server *srv = objt_server(s->target);
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100949 struct conn_src *src;
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200950 struct connection *cli_conn;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200951 struct connection *srv_conn = objt_conn(s->req->cons->end);
Willy Tarreau827aee92011-03-10 16:55:02 +0100952
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100953 if (srv && srv->conn_src.opts & CO_SRC_BIND)
954 src = &srv->conn_src;
955 else if (s->be->conn_src.opts & CO_SRC_BIND)
956 src = &s->be->conn_src;
957 else
958 return;
Willy Tarreau294c4732011-12-16 21:35:50 +0100959
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100960 switch (src->opts & CO_SRC_TPROXY_MASK) {
961 case CO_SRC_TPROXY_ADDR:
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200962 srv_conn->addr.from = src->tproxy_addr;
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100963 break;
964 case CO_SRC_TPROXY_CLI:
965 case CO_SRC_TPROXY_CIP:
966 /* FIXME: what can we do if the client connects in IPv6 or unix socket ? */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200967 cli_conn = objt_conn(s->req->prod->end);
968 if (cli_conn)
969 srv_conn->addr.from = cli_conn->addr.from;
970 else
971 memset(&srv_conn->addr.from, 0, sizeof(srv_conn->addr.from));
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100972 break;
973 case CO_SRC_TPROXY_DYN:
974 if (src->bind_hdr_occ) {
975 char *vptr;
976 int vlen;
977 int rewind;
Willy Tarreau294c4732011-12-16 21:35:50 +0100978
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100979 /* bind to the IP in a header */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200980 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_family = AF_INET;
981 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_port = 0;
982 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_addr.s_addr = 0;
Willy Tarreau294c4732011-12-16 21:35:50 +0100983
Willy Tarreau211cdec2014-04-17 20:18:08 +0200984 b_rew(s->req->buf, rewind = http_hdr_rewind(&s->txn.req));
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100985 if (http_get_hdr(&s->txn.req, src->bind_hdr_name, src->bind_hdr_len,
986 &s->txn.hdr_idx, src->bind_hdr_occ, NULL, &vptr, &vlen)) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200987 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_addr.s_addr =
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100988 htonl(inetaddr_host_lim(vptr, vptr + vlen));
Willy Tarreaubce70882009-09-07 11:51:47 +0200989 }
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100990 b_adv(s->req->buf, rewind);
Willy Tarreaub1d67742010-03-29 19:36:59 +0200991 }
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100992 break;
993 default:
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200994 memset(&srv_conn->addr.from, 0, sizeof(srv_conn->addr.from));
Willy Tarreaub1d67742010-03-29 19:36:59 +0200995 }
996#endif
997}
998
999
Willy Tarreaubaaee002006-06-26 02:48:02 +02001000/*
1001 * This function initiates a connection to the server assigned to this session
Willy Tarreau6471afb2011-09-23 10:54:59 +02001002 * (s->target, s->req->cons->addr.to). It will assign a server if none
Willy Tarreau664beb82011-03-10 11:38:29 +01001003 * is assigned yet.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001004 * It can return one of :
1005 * - SN_ERR_NONE if everything's OK
1006 * - SN_ERR_SRVTO if there are no more servers
1007 * - SN_ERR_SRVCL if the connection was refused by the server
1008 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1009 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1010 * - SN_ERR_INTERNAL for any other purely internal errors
1011 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001012 * The server-facing stream interface is expected to hold a pre-allocated connection
1013 * in s->req->cons->conn.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001014 */
1015int connect_server(struct session *s)
1016{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001017 struct connection *cli_conn;
Willy Tarreau34601a82013-12-15 16:33:46 +01001018 struct connection *srv_conn;
Willy Tarreau827aee92011-03-10 16:55:02 +01001019 struct server *srv;
Willy Tarreau34601a82013-12-15 16:33:46 +01001020 int reuse = 0;
Willy Tarreau9650f372009-08-16 14:02:45 +02001021 int err;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001022
Willy Tarreau34601a82013-12-15 16:33:46 +01001023 srv_conn = objt_conn(s->req->cons->end);
1024 if (srv_conn)
1025 reuse = s->target == srv_conn->target;
1026
1027 if (reuse) {
1028 /* Disable connection reuse if a dynamic source is used.
1029 * As long as we don't share connections between servers,
1030 * we don't need to disable connection reuse on no-idempotent
1031 * requests nor when PROXY protocol is used.
1032 */
1033 srv = objt_server(s->target);
1034 if (srv && srv->conn_src.opts & CO_SRC_BIND) {
1035 if ((srv->conn_src.opts & CO_SRC_TPROXY_MASK) == CO_SRC_TPROXY_DYN)
1036 reuse = 0;
1037 }
1038 else if (s->be->conn_src.opts & CO_SRC_BIND) {
1039 if ((s->be->conn_src.opts & CO_SRC_TPROXY_MASK) == CO_SRC_TPROXY_DYN)
1040 reuse = 0;
1041 }
1042 }
1043
1044 srv_conn = si_alloc_conn(s->req->cons, reuse);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001045 if (!srv_conn)
1046 return SN_ERR_RESOURCE;
1047
Willy Tarreaubaaee002006-06-26 02:48:02 +02001048 if (!(s->flags & SN_ADDR_SET)) {
1049 err = assign_server_address(s);
1050 if (err != SRV_STATUS_OK)
1051 return SN_ERR_INTERNAL;
1052 }
1053
Willy Tarreauff605db2013-12-20 11:09:51 +01001054 if (!conn_xprt_ready(srv_conn)) {
1055 /* the target was only on the session, assign it to the SI now */
1056 srv_conn->target = s->target;
Willy Tarreaud88edf22009-06-14 15:48:17 +02001057
Willy Tarreauff605db2013-12-20 11:09:51 +01001058 /* set the correct protocol on the output stream interface */
1059 if (objt_server(s->target)) {
1060 conn_prepare(srv_conn, objt_server(s->target)->proto, objt_server(s->target)->xprt);
1061 }
1062 else if (obj_type(s->target) == OBJ_TYPE_PROXY) {
1063 /* proxies exclusively run on raw_sock right now */
1064 conn_prepare(srv_conn, protocol_by_family(srv_conn->addr.to.ss_family), &raw_sock);
1065 if (!objt_conn(s->req->cons->end) || !objt_conn(s->req->cons->end)->ctrl)
1066 return SN_ERR_INTERNAL;
1067 }
1068 else
1069 return SN_ERR_INTERNAL; /* how did we get there ? */
Willy Tarreaud02394b2012-05-11 18:32:18 +02001070
Willy Tarreauff605db2013-12-20 11:09:51 +01001071 /* process the case where the server requires the PROXY protocol to be sent */
1072 srv_conn->send_proxy_ofs = 0;
David Safb76832014-05-08 23:42:08 -04001073 if (objt_server(s->target) && objt_server(s->target)->pp_opts) {
Willy Tarreauff605db2013-12-20 11:09:51 +01001074 srv_conn->send_proxy_ofs = 1; /* must compute size */
1075 cli_conn = objt_conn(s->req->prod->end);
1076 if (cli_conn)
1077 conn_get_to_addr(cli_conn);
1078 }
Willy Tarreau2a6e8802013-10-24 15:50:53 +02001079
Willy Tarreauff605db2013-12-20 11:09:51 +01001080 si_attach_conn(s->req->cons, srv_conn);
Willy Tarreau26d8c592012-05-07 18:12:14 +02001081
Willy Tarreauff605db2013-12-20 11:09:51 +01001082 assign_tproxy_address(s);
1083 }
1084 else {
1085 /* the connection is being reused, just re-attach it */
1086 si_attach_conn(s->req->cons, srv_conn);
Willy Tarreau36346242014-02-24 18:26:30 +01001087 s->flags |= SN_SRV_REUSED;
Willy Tarreauff605db2013-12-20 11:09:51 +01001088 }
Willy Tarreaub1d67742010-03-29 19:36:59 +02001089
William Lallemandb7ff6a32012-03-02 14:35:21 +01001090 /* flag for logging source ip/port */
1091 if (s->fe->options2 & PR_O2_SRC_ADDR)
1092 s->req->cons->flags |= SI_FL_SRC_ADDR;
1093
Willy Tarreau14f8e862012-08-30 22:23:13 +02001094 /* disable lingering */
1095 if (s->be->options & PR_O_TCP_NOLING)
1096 s->req->cons->flags |= SI_FL_NOLINGER;
1097
Willy Tarreau73b013b2012-05-21 16:31:45 +02001098 err = si_connect(s->req->cons);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001099
Willy Tarreau9650f372009-08-16 14:02:45 +02001100 if (err != SN_ERR_NONE)
1101 return err;
Willy Tarreau788e2842008-08-26 13:25:39 +02001102
Willy Tarreau14f8e862012-08-30 22:23:13 +02001103 /* set connect timeout */
1104 s->req->cons->exp = tick_add_ifset(now_ms, s->be->timeout.connect);
1105
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001106 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001107 if (srv) {
Willy Tarreau1e62de62008-11-11 20:20:02 +01001108 s->flags |= SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001109 srv->cur_sess++;
1110 if (srv->cur_sess > srv->counters.cur_sess_max)
1111 srv->counters.cur_sess_max = srv->cur_sess;
Willy Tarreau51406232008-03-10 22:04:20 +01001112 if (s->be->lbprm.server_take_conn)
Willy Tarreau827aee92011-03-10 16:55:02 +01001113 s->be->lbprm.server_take_conn(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001114 }
1115
Willy Tarreaubaaee002006-06-26 02:48:02 +02001116 return SN_ERR_NONE; /* connection is OK */
1117}
1118
1119
Willy Tarreaubaaee002006-06-26 02:48:02 +02001120/* This function performs the "redispatch" part of a connection attempt. It
1121 * will assign a server if required, queue the connection if required, and
1122 * handle errors that might arise at this level. It can change the server
1123 * state. It will return 1 if it encounters an error, switches the server
1124 * state, or has to queue a connection. Otherwise, it will return 0 indicating
1125 * that the connection is ready to use.
1126 */
1127
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001128int srv_redispatch_connect(struct session *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001129{
Willy Tarreau827aee92011-03-10 16:55:02 +01001130 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001131 int conn_err;
1132
1133 /* We know that we don't have any connection pending, so we will
1134 * try to get a new one, and wait in this state if it's queued
1135 */
Willy Tarreau7c669d72008-06-20 15:04:11 +02001136 redispatch:
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001137 conn_err = assign_server_and_queue(s);
1138 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001139
Willy Tarreaubaaee002006-06-26 02:48:02 +02001140 switch (conn_err) {
1141 case SRV_STATUS_OK:
1142 break;
1143
Willy Tarreau7c669d72008-06-20 15:04:11 +02001144 case SRV_STATUS_FULL:
1145 /* The server has reached its maxqueue limit. Either PR_O_REDISP is set
1146 * and we can redispatch to another server, or it is not and we return
1147 * 503. This only makes sense in DIRECT mode however, because normal LB
1148 * algorithms would never select such a server, and hash algorithms
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001149 * would bring us on the same server again. Note that s->target is set
Willy Tarreau827aee92011-03-10 16:55:02 +01001150 * in this case.
Willy Tarreau7c669d72008-06-20 15:04:11 +02001151 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001152 if (((s->flags & (SN_DIRECT|SN_FORCE_PRST)) == SN_DIRECT) &&
1153 (s->be->options & PR_O_REDISP)) {
1154 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Willy Tarreau7c669d72008-06-20 15:04:11 +02001155 goto redispatch;
1156 }
1157
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001158 if (!s->req->cons->err_type) {
1159 s->req->cons->err_type = SI_ET_QUEUE_ERR;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001160 }
Willy Tarreau7c669d72008-06-20 15:04:11 +02001161
Willy Tarreau827aee92011-03-10 16:55:02 +01001162 srv->counters.failed_conns++;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001163 s->be->be_counters.failed_conns++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02001164 return 1;
1165
Willy Tarreaubaaee002006-06-26 02:48:02 +02001166 case SRV_STATUS_NOSRV:
Willy Tarreau827aee92011-03-10 16:55:02 +01001167 /* note: it is guaranteed that srv == NULL here */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001168 if (!s->req->cons->err_type) {
1169 s->req->cons->err_type = SI_ET_CONN_ERR;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001170 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +01001171
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001172 s->be->be_counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001173 return 1;
1174
1175 case SRV_STATUS_QUEUED:
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001176 s->req->cons->exp = tick_add_ifset(now_ms, s->be->timeout.queue);
1177 s->req->cons->state = SI_ST_QUE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001178 /* do nothing else and do not wake any other session up */
1179 return 1;
1180
Willy Tarreaubaaee002006-06-26 02:48:02 +02001181 case SRV_STATUS_INTERNAL:
1182 default:
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001183 if (!s->req->cons->err_type) {
1184 s->req->cons->err_type = SI_ET_CONN_OTHER;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001185 }
1186
Willy Tarreau827aee92011-03-10 16:55:02 +01001187 if (srv)
1188 srv_inc_sess_ctr(srv);
1189 if (srv)
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001190 srv_set_sess_last(srv);
1191 if (srv)
Willy Tarreau827aee92011-03-10 16:55:02 +01001192 srv->counters.failed_conns++;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001193 s->be->be_counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001194
1195 /* release other sessions waiting for this server */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001196 if (may_dequeue_tasks(srv, s->be))
Willy Tarreau827aee92011-03-10 16:55:02 +01001197 process_srv_queue(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001198 return 1;
1199 }
1200 /* if we get here, it's because we got SRV_STATUS_OK, which also
1201 * means that the connection has not been queued.
1202 */
1203 return 0;
1204}
1205
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001206/* sends a log message when a backend goes down, and also sets last
1207 * change date.
1208 */
1209void set_backend_down(struct proxy *be)
1210{
1211 be->last_change = now.tv_sec;
1212 be->down_trans++;
1213
1214 Alert("%s '%s' has no server available!\n", proxy_type_str(be), be->id);
1215 send_log(be, LOG_EMERG, "%s %s has no server available!\n", proxy_type_str(be), be->id);
1216}
1217
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001218/* Apply RDP cookie persistence to the current session. For this, the function
1219 * tries to extract an RDP cookie from the request buffer, and look for the
1220 * matching server in the list. If the server is found, it is assigned to the
1221 * session. This always returns 1, and the analyser removes itself from the
1222 * list. Nothing is performed if a server was already assigned.
1223 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02001224int tcp_persist_rdp_cookie(struct session *s, struct channel *req, int an_bit)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001225{
1226 struct proxy *px = s->be;
1227 int ret;
Willy Tarreau37406352012-04-23 16:16:37 +02001228 struct sample smp;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001229 struct server *srv = px->srv;
1230 struct sockaddr_in addr;
1231 char *p;
1232
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001233 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 +02001234 now_ms, __FUNCTION__,
1235 s,
1236 req,
1237 req->rex, req->wex,
1238 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001239 req->buf->i,
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001240 req->analysers);
1241
1242 if (s->flags & SN_ASSIGNED)
1243 goto no_cookie;
1244
Willy Tarreau37406352012-04-23 16:16:37 +02001245 memset(&smp, 0, sizeof(smp));
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001246
Willy Tarreaucadd8c92013-07-22 18:09:52 +02001247 ret = fetch_rdp_cookie_name(s, &smp, s->be->rdp_cookie_name, s->be->rdp_cookie_len);
Willy Tarreauf853c462012-04-23 18:53:56 +02001248 if (ret == 0 || (smp.flags & SMP_F_MAY_CHANGE) || smp.data.str.len == 0)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001249 goto no_cookie;
1250
1251 memset(&addr, 0, sizeof(addr));
1252 addr.sin_family = AF_INET;
1253
Willy Tarreau664092c2011-12-16 19:11:42 +01001254 /* Considering an rdp cookie detected using acl, str ended with <cr><lf> and should return */
Willy Tarreauf853c462012-04-23 18:53:56 +02001255 addr.sin_addr.s_addr = strtoul(smp.data.str.str, &p, 10);
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001256 if (*p != '.')
1257 goto no_cookie;
1258 p++;
1259 addr.sin_port = (unsigned short)strtoul(p, &p, 10);
1260 if (*p != '.')
1261 goto no_cookie;
1262
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001263 s->target = NULL;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001264 while (srv) {
Willy Tarreau28e9d062014-05-09 22:47:50 +02001265 if (srv->addr.ss_family == AF_INET &&
1266 memcmp(&addr, &(srv->addr), sizeof(addr)) == 0) {
Willy Tarreau892337c2014-05-13 23:41:20 +02001267 if ((srv->state != SRV_ST_STOPPED) || (px->options & PR_O_PERSIST)) {
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001268 /* we found the server and it is usable */
1269 s->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001270 s->target = &srv->obj_type;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001271 break;
1272 }
1273 }
1274 srv = srv->next;
1275 }
1276
1277no_cookie:
1278 req->analysers &= ~an_bit;
1279 req->analyse_exp = TICK_ETERNITY;
1280 return 1;
1281}
1282
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001283int be_downtime(struct proxy *px) {
Willy Tarreaub625a082007-11-26 01:15:43 +01001284 if (px->lbprm.tot_weight && px->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001285 return px->down_time;
1286
1287 return now.tv_sec - px->last_change + px->down_time;
1288}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001289
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001290/*
1291 * This function returns a string containing the balancing
1292 * mode of the proxy in a format suitable for stats.
1293 */
1294
1295const char *backend_lb_algo_str(int algo) {
1296
1297 if (algo == BE_LB_ALGO_RR)
1298 return "roundrobin";
1299 else if (algo == BE_LB_ALGO_SRR)
1300 return "static-rr";
Willy Tarreauf09c6602012-02-13 17:12:08 +01001301 else if (algo == BE_LB_ALGO_FAS)
1302 return "first";
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001303 else if (algo == BE_LB_ALGO_LC)
1304 return "leastconn";
1305 else if (algo == BE_LB_ALGO_SH)
1306 return "source";
1307 else if (algo == BE_LB_ALGO_UH)
1308 return "uri";
1309 else if (algo == BE_LB_ALGO_PH)
1310 return "url_param";
1311 else if (algo == BE_LB_ALGO_HH)
1312 return "hdr";
1313 else if (algo == BE_LB_ALGO_RCH)
1314 return "rdp-cookie";
1315 else
1316 return NULL;
1317}
1318
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001319/* This function parses a "balance" statement in a backend section describing
1320 * <curproxy>. It returns -1 if there is any error, otherwise zero. If it
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001321 * returns -1, it will write an error message into the <err> buffer which will
1322 * automatically be allocated and must be passed as NULL. The trailing '\n'
1323 * will not be written. The function must be called with <args> pointing to the
1324 * first word after "balance".
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001325 */
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001326int backend_parse_balance(const char **args, char **err, struct proxy *curproxy)
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001327{
1328 if (!*(args[0])) {
1329 /* if no option is set, use round-robin by default */
Willy Tarreau31682232007-11-29 15:38:04 +01001330 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1331 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001332 return 0;
1333 }
1334
1335 if (!strcmp(args[0], "roundrobin")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001336 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1337 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001338 }
Willy Tarreau9757a382009-10-03 12:56:50 +02001339 else if (!strcmp(args[0], "static-rr")) {
1340 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1341 curproxy->lbprm.algo |= BE_LB_ALGO_SRR;
1342 }
Willy Tarreauf09c6602012-02-13 17:12:08 +01001343 else if (!strcmp(args[0], "first")) {
1344 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1345 curproxy->lbprm.algo |= BE_LB_ALGO_FAS;
1346 }
Willy Tarreau51406232008-03-10 22:04:20 +01001347 else if (!strcmp(args[0], "leastconn")) {
1348 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1349 curproxy->lbprm.algo |= BE_LB_ALGO_LC;
1350 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001351 else if (!strcmp(args[0], "source")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001352 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1353 curproxy->lbprm.algo |= BE_LB_ALGO_SH;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001354 }
1355 else if (!strcmp(args[0], "uri")) {
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001356 int arg = 1;
1357
Willy Tarreau31682232007-11-29 15:38:04 +01001358 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1359 curproxy->lbprm.algo |= BE_LB_ALGO_UH;
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001360
Oskar Stolc8dc41842012-05-19 10:19:54 +01001361 curproxy->uri_whole = 0;
1362
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001363 while (*args[arg]) {
1364 if (!strcmp(args[arg], "len")) {
1365 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001366 memprintf(err, "%s : '%s' expects a positive integer (got '%s').", args[0], args[arg], args[arg+1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001367 return -1;
1368 }
1369 curproxy->uri_len_limit = atoi(args[arg+1]);
1370 arg += 2;
1371 }
1372 else if (!strcmp(args[arg], "depth")) {
1373 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001374 memprintf(err, "%s : '%s' expects a positive integer (got '%s').", args[0], args[arg], args[arg+1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001375 return -1;
1376 }
1377 /* hint: we store the position of the ending '/' (depth+1) so
1378 * that we avoid a comparison while computing the hash.
1379 */
1380 curproxy->uri_dirs_depth1 = atoi(args[arg+1]) + 1;
1381 arg += 2;
1382 }
Oskar Stolc8dc41842012-05-19 10:19:54 +01001383 else if (!strcmp(args[arg], "whole")) {
1384 curproxy->uri_whole = 1;
1385 arg += 1;
1386 }
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001387 else {
Oskar Stolc8dc41842012-05-19 10:19:54 +01001388 memprintf(err, "%s only accepts parameters 'len', 'depth', and 'whole' (got '%s').", args[0], args[arg]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001389 return -1;
1390 }
1391 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001392 }
Willy Tarreau01732802007-11-01 22:48:15 +01001393 else if (!strcmp(args[0], "url_param")) {
1394 if (!*args[1]) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001395 memprintf(err, "%s requires an URL parameter name.", args[0]);
Willy Tarreau01732802007-11-01 22:48:15 +01001396 return -1;
1397 }
Willy Tarreau31682232007-11-29 15:38:04 +01001398 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1399 curproxy->lbprm.algo |= BE_LB_ALGO_PH;
Willy Tarreaua534fea2008-08-03 12:19:50 +02001400
1401 free(curproxy->url_param_name);
Willy Tarreau01732802007-11-01 22:48:15 +01001402 curproxy->url_param_name = strdup(args[1]);
Willy Tarreaua534fea2008-08-03 12:19:50 +02001403 curproxy->url_param_len = strlen(args[1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001404 if (*args[2]) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001405 if (strcmp(args[2], "check_post")) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001406 memprintf(err, "%s only accepts 'check_post' modifier (got '%s').", args[0], args[2]);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001407 return -1;
1408 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001409 }
Benoitaffb4812009-03-25 13:02:10 +01001410 }
1411 else if (!strncmp(args[0], "hdr(", 4)) {
1412 const char *beg, *end;
1413
1414 beg = args[0] + 4;
1415 end = strchr(beg, ')');
1416
1417 if (!end || end == beg) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001418 memprintf(err, "hdr requires an http header field name.");
Benoitaffb4812009-03-25 13:02:10 +01001419 return -1;
1420 }
1421
1422 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1423 curproxy->lbprm.algo |= BE_LB_ALGO_HH;
1424
1425 free(curproxy->hh_name);
1426 curproxy->hh_len = end - beg;
1427 curproxy->hh_name = my_strndup(beg, end - beg);
1428 curproxy->hh_match_domain = 0;
1429
1430 if (*args[1]) {
1431 if (strcmp(args[1], "use_domain_only")) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001432 memprintf(err, "%s only accepts 'use_domain_only' modifier (got '%s').", args[0], args[1]);
Benoitaffb4812009-03-25 13:02:10 +01001433 return -1;
1434 }
1435 curproxy->hh_match_domain = 1;
1436 }
Emeric Brun736aa232009-06-30 17:56:00 +02001437 }
1438 else if (!strncmp(args[0], "rdp-cookie", 10)) {
1439 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1440 curproxy->lbprm.algo |= BE_LB_ALGO_RCH;
1441
1442 if ( *(args[0] + 10 ) == '(' ) { /* cookie name */
1443 const char *beg, *end;
1444
1445 beg = args[0] + 11;
1446 end = strchr(beg, ')');
1447
1448 if (!end || end == beg) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001449 memprintf(err, "rdp-cookie : missing cookie name.");
Emeric Brun736aa232009-06-30 17:56:00 +02001450 return -1;
1451 }
1452
1453 free(curproxy->hh_name);
1454 curproxy->hh_name = my_strndup(beg, end - beg);
1455 curproxy->hh_len = end - beg;
1456 }
1457 else if ( *(args[0] + 10 ) == '\0' ) { /* default cookie name 'mstshash' */
1458 free(curproxy->hh_name);
1459 curproxy->hh_name = strdup("mstshash");
1460 curproxy->hh_len = strlen(curproxy->hh_name);
1461 }
1462 else { /* syntax */
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001463 memprintf(err, "rdp-cookie : missing cookie name.");
Emeric Brun736aa232009-06-30 17:56:00 +02001464 return -1;
1465 }
Willy Tarreau01732802007-11-01 22:48:15 +01001466 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001467 else {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001468 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 +01001469 return -1;
1470 }
1471 return 0;
1472}
1473
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001474
1475/************************************************************************/
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001476/* All supported sample and ACL keywords must be declared here. */
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001477/************************************************************************/
1478
Willy Tarreau34db1082012-04-19 17:16:54 +02001479/* set temp integer to the number of enabled servers on the proxy.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001480 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001481 * undefined behaviour.
1482 */
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001483static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001484smp_fetch_nbsrv(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001485 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001486{
Willy Tarreau37406352012-04-23 16:16:37 +02001487 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001488 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001489 px = args->data.prx;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001490
1491 if (px->srv_act)
Willy Tarreauf853c462012-04-23 18:53:56 +02001492 smp->data.uint = px->srv_act;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001493 else if (px->lbprm.fbck)
Willy Tarreauf853c462012-04-23 18:53:56 +02001494 smp->data.uint = 1;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001495 else
Willy Tarreauf853c462012-04-23 18:53:56 +02001496 smp->data.uint = px->srv_bck;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001497
1498 return 1;
1499}
1500
Willy Tarreau37406352012-04-23 16:16:37 +02001501/* report in smp->flags a success or failure depending on the designated
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001502 * server's state. There is no match function involved since there's no pattern.
Willy Tarreau34db1082012-04-19 17:16:54 +02001503 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1504 * undefined behaviour.
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001505 */
1506static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001507smp_fetch_srv_is_up(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001508 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001509{
Willy Tarreau24e32d82012-04-23 23:55:44 +02001510 struct server *srv = args->data.srv;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001511
Willy Tarreau37406352012-04-23 16:16:37 +02001512 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001513 smp->type = SMP_T_BOOL;
Willy Tarreau20125212014-05-13 19:44:56 +02001514 if (!(srv->admin & SRV_ADMF_MAINT) &&
Willy Tarreau892337c2014-05-13 23:41:20 +02001515 (!(srv->check.state & CHK_ST_CONFIGURED) || (srv->state != SRV_ST_STOPPED)))
Willy Tarreau197e10a2012-04-23 19:18:42 +02001516 smp->data.uint = 1;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001517 else
Willy Tarreau197e10a2012-04-23 19:18:42 +02001518 smp->data.uint = 0;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001519 return 1;
1520}
1521
Willy Tarreau34db1082012-04-19 17:16:54 +02001522/* set temp integer to the number of enabled servers on the proxy.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001523 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001524 * undefined behaviour.
1525 */
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001526static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001527smp_fetch_connslots(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)
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001529{
1530 struct server *iterator;
Willy Tarreaud28c3532012-04-19 19:28:33 +02001531
Willy Tarreau37406352012-04-23 16:16:37 +02001532 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001533 smp->type = SMP_T_UINT;
1534 smp->data.uint = 0;
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001535
Willy Tarreau24e32d82012-04-23 23:55:44 +02001536 for (iterator = args->data.prx->srv; iterator; iterator = iterator->next) {
Willy Tarreau892337c2014-05-13 23:41:20 +02001537 if (iterator->state == SRV_ST_STOPPED)
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001538 continue;
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001539
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001540 if (iterator->maxconn == 0 || iterator->maxqueue == 0) {
Willy Tarreaua5e37562011-12-16 17:06:15 +01001541 /* configuration is stupid */
Willy Tarreauf853c462012-04-23 18:53:56 +02001542 smp->data.uint = -1; /* FIXME: stupid value! */
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001543 return 1;
1544 }
1545
Willy Tarreauf853c462012-04-23 18:53:56 +02001546 smp->data.uint += (iterator->maxconn - iterator->cur_sess)
Willy Tarreau422aa072012-04-20 20:49:27 +02001547 + (iterator->maxqueue - iterator->nbpend);
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001548 }
1549
1550 return 1;
1551}
1552
Willy Tarreaua5e37562011-12-16 17:06:15 +01001553/* set temp integer to the id of the backend */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001554static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001555smp_fetch_be_id(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 Tarreau37406352012-04-23 16:16:37 +02001557{
Willy Tarreauf853c462012-04-23 18:53:56 +02001558 smp->flags = SMP_F_VOL_TXN;
1559 smp->type = SMP_T_UINT;
1560 smp->data.uint = l4->be->uuid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001561 return 1;
1562}
1563
Willy Tarreaua5e37562011-12-16 17:06:15 +01001564/* set temp integer to the id of the server */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001565static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001566smp_fetch_srv_id(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001567 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau37406352012-04-23 16:16:37 +02001568{
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001569 if (!objt_server(l4->target))
Willy Tarreau17af4192011-02-23 14:27:06 +01001570 return 0;
1571
Willy Tarreauf853c462012-04-23 18:53:56 +02001572 smp->type = SMP_T_UINT;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001573 smp->data.uint = objt_server(l4->target)->puid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001574
1575 return 1;
1576}
1577
Willy Tarreau34db1082012-04-19 17:16:54 +02001578/* set temp integer to the number of connections per second reaching 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 Tarreau079ff0a2009-03-05 21:34:28 +01001582static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001583smp_fetch_be_sess_rate(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 Tarreau079ff0a2009-03-05 21:34:28 +01001585{
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 = read_freq_ctr(&args->data.prx->be_sess_per_sec);
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001589 return 1;
1590}
1591
Willy Tarreau34db1082012-04-19 17:16:54 +02001592/* set temp integer to the number of concurrent connections on the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001593 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001594 * undefined behaviour.
1595 */
Willy Tarreaua36af912009-10-10 12:02:45 +02001596static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001597smp_fetch_be_conn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001598 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua36af912009-10-10 12:02:45 +02001599{
Willy Tarreau37406352012-04-23 16:16:37 +02001600 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001601 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001602 smp->data.uint = args->data.prx->beconn;
Willy Tarreaua36af912009-10-10 12:02:45 +02001603 return 1;
1604}
1605
Willy Tarreau34db1082012-04-19 17:16:54 +02001606/* set temp integer to the total number of queued connections on the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001607 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001608 * undefined behaviour.
1609 */
Willy Tarreaua36af912009-10-10 12:02:45 +02001610static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001611smp_fetch_queue_size(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001612 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua36af912009-10-10 12:02:45 +02001613{
Willy Tarreau37406352012-04-23 16:16:37 +02001614 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001615 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001616 smp->data.uint = args->data.prx->totpend;
Willy Tarreaua36af912009-10-10 12:02:45 +02001617 return 1;
1618}
1619
Willy Tarreaua5e37562011-12-16 17:06:15 +01001620/* set temp integer to the total number of queued connections on the backend divided
Willy Tarreaua36af912009-10-10 12:02:45 +02001621 * by the number of running servers and rounded up. If there is no running
1622 * server, we return twice the total, just as if we had half a running server.
1623 * This is more or less correct anyway, since we expect the last server to come
1624 * back soon.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001625 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001626 * undefined behaviour.
Willy Tarreaua36af912009-10-10 12:02:45 +02001627 */
1628static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001629smp_fetch_avg_queue_size(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001630 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua36af912009-10-10 12:02:45 +02001631{
1632 int nbsrv;
1633
Willy Tarreau37406352012-04-23 16:16:37 +02001634 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001635 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001636 px = args->data.prx;
Willy Tarreaua36af912009-10-10 12:02:45 +02001637
1638 if (px->srv_act)
1639 nbsrv = px->srv_act;
1640 else if (px->lbprm.fbck)
1641 nbsrv = 1;
1642 else
1643 nbsrv = px->srv_bck;
1644
1645 if (nbsrv > 0)
Willy Tarreauf853c462012-04-23 18:53:56 +02001646 smp->data.uint = (px->totpend + nbsrv - 1) / nbsrv;
Willy Tarreaua36af912009-10-10 12:02:45 +02001647 else
Willy Tarreauf853c462012-04-23 18:53:56 +02001648 smp->data.uint = px->totpend * 2;
Willy Tarreaua36af912009-10-10 12:02:45 +02001649
1650 return 1;
1651}
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001652
Willy Tarreau34db1082012-04-19 17:16:54 +02001653/* set temp integer to the number of concurrent connections on the server in the backend.
1654 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1655 * undefined behaviour.
1656 */
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001657static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001658smp_fetch_srv_conn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001659 const struct arg *args, struct sample *smp, const char *kw)
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001660{
Willy Tarreauf853c462012-04-23 18:53:56 +02001661 smp->flags = SMP_F_VOL_TEST;
1662 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001663 smp->data.uint = args->data.srv->cur_sess;
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001664 return 1;
1665}
1666
Tait Clarridge7896d522012-12-05 21:39:31 -05001667/* set temp integer to the number of enabled servers on the proxy.
1668 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1669 * undefined behaviour.
1670 */
1671static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001672smp_fetch_srv_sess_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001673 const struct arg *args, struct sample *smp, const char *kw)
Tait Clarridge7896d522012-12-05 21:39:31 -05001674{
1675 smp->flags = SMP_F_VOL_TEST;
1676 smp->type = SMP_T_UINT;
1677 smp->data.uint = read_freq_ctr(&args->data.srv->sess_per_sec);
1678 return 1;
1679}
1680
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001681
1682/* Note: must not be declared <const> as its list will be overwritten.
1683 * Please take care of keeping this list alphabetically sorted.
1684 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001685static struct sample_fetch_kw_list smp_kws = {ILH, {
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001686 { "avg_queue", smp_fetch_avg_queue_size, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1687 { "be_conn", smp_fetch_be_conn, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1688 { "be_id", smp_fetch_be_id, 0, NULL, SMP_T_UINT, SMP_USE_BKEND, },
1689 { "be_sess_rate", smp_fetch_be_sess_rate, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1690 { "connslots", smp_fetch_connslots, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1691 { "nbsrv", smp_fetch_nbsrv, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1692 { "queue", smp_fetch_queue_size, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1693 { "srv_conn", smp_fetch_srv_conn, ARG1(1,SRV), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1694 { "srv_id", smp_fetch_srv_id, 0, NULL, SMP_T_UINT, SMP_USE_SERVR, },
1695 { "srv_is_up", smp_fetch_srv_is_up, ARG1(1,SRV), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
1696 { "srv_sess_rate", smp_fetch_srv_sess_rate, ARG1(1,SRV), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1697 { /* END */ },
1698}};
1699
1700
Willy Tarreau61612d42012-04-19 18:42:05 +02001701/* Note: must not be declared <const> as its list will be overwritten.
1702 * Please take care of keeping this list alphabetically sorted.
1703 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001704static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001705 { /* END */ },
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001706}};
1707
1708
1709__attribute__((constructor))
1710static void __backend_init(void)
1711{
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001712 sample_register_fetches(&smp_kws);
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001713 acl_register_keywords(&acl_kws);
1714}
1715
Willy Tarreaubaaee002006-06-26 02:48:02 +02001716/*
1717 * Local variables:
1718 * c-indent-level: 8
1719 * c-basic-offset: 8
1720 * End:
1721 */