blob: 14908ddd7612796ee45f19ab2d29696dd2b35bb9 [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;
natalie.chendf7a7142015-03-31 15:54:07 +0800190 int depth = 1;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200191
192 if (px->lbprm.tot_weight == 0)
193 return NULL;
194
195 /* note: we won't hash if there's only one server left */
196 if (px->lbprm.tot_used == 1)
197 goto hash_done;
198
199 if (px->uri_len_limit)
200 uri_len = MIN(uri_len, px->uri_len_limit);
201
natalie.chendf7a7142015-03-31 15:54:07 +0800202 start = uri;
203 end = uri + uri_len - 1;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200204 while (uri_len--) {
Willy Tarreauac032922014-10-17 12:11:50 +0200205 c = *end;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200206 if (c == '/') {
207 slashes++;
natalie.chendf7a7142015-03-31 15:54:07 +0800208 if (slashes == depth)
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200209 break;
210 }
natalie.chendf7a7142015-03-31 15:54:07 +0800211 else if (c == '?')
212 depth = 2;
213 end--;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200214 }
Bhaskar98634f02013-10-29 23:30:51 -0400215
216 hash = gen_hash(px, start, (end - start));
217
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500218 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100219 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200220 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200221 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
222 return chash_get_server_hash(px, hash);
223 else
224 return map_get_server_hash(px, hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200225}
226
Willy Tarreau01732802007-11-01 22:48:15 +0100227/*
228 * This function tries to find a running server for the proxy <px> following
229 * the URL parameter hash method. It looks for a specific parameter in the
230 * URL and hashes it to compute the server ID. This is useful to optimize
231 * performance by avoiding bounces between servers in contexts where sessions
232 * are shared but cookies are not usable. If the parameter is not found, NULL
233 * is returned. If any server is found, it will be returned. If no valid server
234 * is found, NULL is returned.
Willy Tarreau01732802007-11-01 22:48:15 +0100235 */
236struct server *get_server_ph(struct proxy *px, const char *uri, int uri_len)
237{
Dan Dubovik82456752014-07-08 08:51:03 -0700238 unsigned int hash = 0;
Bhaskar98634f02013-10-29 23:30:51 -0400239 const char *start, *end;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200240 const char *p;
241 const char *params;
Willy Tarreau01732802007-11-01 22:48:15 +0100242 int plen;
243
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200244 /* when tot_weight is 0 then so is srv_count */
Willy Tarreau20697042007-11-15 23:26:18 +0100245 if (px->lbprm.tot_weight == 0)
Willy Tarreau01732802007-11-01 22:48:15 +0100246 return NULL;
247
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200248 if ((p = memchr(uri, '?', uri_len)) == NULL)
249 return NULL;
250
Willy Tarreau01732802007-11-01 22:48:15 +0100251 p++;
252
253 uri_len -= (p - uri);
254 plen = px->url_param_len;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200255 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100256
257 while (uri_len > plen) {
258 /* Look for the parameter name followed by an equal symbol */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200259 if (params[plen] == '=') {
260 if (memcmp(params, px->url_param_name, plen) == 0) {
261 /* OK, we have the parameter here at <params>, and
Willy Tarreau01732802007-11-01 22:48:15 +0100262 * the value after the equal sign, at <p>
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200263 * skip the equal symbol
Willy Tarreau01732802007-11-01 22:48:15 +0100264 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200265 p += plen + 1;
Bhaskar98634f02013-10-29 23:30:51 -0400266 start = end = p;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200267 uri_len -= plen + 1;
268
Bhaskar98634f02013-10-29 23:30:51 -0400269 while (uri_len && *end != '&') {
Willy Tarreau01732802007-11-01 22:48:15 +0100270 uri_len--;
Bhaskar98634f02013-10-29 23:30:51 -0400271 end++;
Willy Tarreau01732802007-11-01 22:48:15 +0100272 }
Bhaskar98634f02013-10-29 23:30:51 -0400273 hash = gen_hash(px, start, (end - start));
274
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500275 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100276 hash = full_hash(hash);
Bhaskar98634f02013-10-29 23:30:51 -0400277
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200278 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
279 return chash_get_server_hash(px, hash);
280 else
281 return map_get_server_hash(px, hash);
Willy Tarreau01732802007-11-01 22:48:15 +0100282 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200283 }
284 /* skip to next parameter */
285 p = memchr(params, '&', uri_len);
286 if (!p)
287 return NULL;
288 p++;
289 uri_len -= (p - params);
290 params = p;
291 }
292 return NULL;
293}
294
295/*
296 * this does the same as the previous server_ph, but check the body contents
297 */
298struct server *get_server_ph_post(struct session *s)
299{
Dan Dubovik82456752014-07-08 08:51:03 -0700300 unsigned int hash = 0;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200301 struct http_txn *txn = &s->txn;
Willy Tarreau7421efb2012-07-02 15:11:27 +0200302 struct channel *req = s->req;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200303 struct http_msg *msg = &txn->req;
304 struct proxy *px = s->be;
305 unsigned int plen = px->url_param_len;
Willy Tarreau2d8e4852014-04-17 20:08:17 +0200306 unsigned long len = http_body_bytes(msg);
Willy Tarreau0d090502014-04-17 20:31:44 +0200307 const char *params = b_ptr(req->buf, -http_data_rewind(msg));
Willy Tarreau157dd632009-12-06 19:18:09 +0100308 const char *p = params;
Bhaskar98634f02013-10-29 23:30:51 -0400309 const char *start, *end;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200310
Willy Tarreau157dd632009-12-06 19:18:09 +0100311 if (len == 0)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200312 return NULL;
313
Willy Tarreau522aab32015-05-02 00:05:47 +0200314 if (len > req->buf->data + req->buf->size - p)
315 len = req->buf->data + req->buf->size - p;
316
Willy Tarreau157dd632009-12-06 19:18:09 +0100317 if (px->lbprm.tot_weight == 0)
318 return NULL;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200319
320 while (len > plen) {
321 /* Look for the parameter name followed by an equal symbol */
322 if (params[plen] == '=') {
323 if (memcmp(params, px->url_param_name, plen) == 0) {
324 /* OK, we have the parameter here at <params>, and
325 * the value after the equal sign, at <p>
326 * skip the equal symbol
327 */
328 p += plen + 1;
Bhaskar98634f02013-10-29 23:30:51 -0400329 start = end = p;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200330 len -= plen + 1;
331
Bhaskar98634f02013-10-29 23:30:51 -0400332 while (len && *end != '&') {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200333 if (unlikely(!HTTP_IS_TOKEN(*p))) {
Willy Tarreau157dd632009-12-06 19:18:09 +0100334 /* if in a POST, body must be URI encoded or it's not a URI.
Bhaskar98634f02013-10-29 23:30:51 -0400335 * Do not interpret any possible binary data as a parameter.
Willy Tarreau157dd632009-12-06 19:18:09 +0100336 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200337 if (likely(HTTP_IS_LWS(*p))) /* eol, uncertain uri len */
338 break;
339 return NULL; /* oh, no; this is not uri-encoded.
340 * This body does not contain parameters.
341 */
342 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200343 len--;
Bhaskar98634f02013-10-29 23:30:51 -0400344 end++;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200345 /* should we break if vlen exceeds limit? */
346 }
Bhaskar98634f02013-10-29 23:30:51 -0400347 hash = gen_hash(px, start, (end - start));
348
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500349 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100350 hash = full_hash(hash);
Bhaskar98634f02013-10-29 23:30:51 -0400351
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200352 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
353 return chash_get_server_hash(px, hash);
354 else
355 return map_get_server_hash(px, hash);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200356 }
357 }
Willy Tarreau01732802007-11-01 22:48:15 +0100358 /* skip to next parameter */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200359 p = memchr(params, '&', len);
Willy Tarreau01732802007-11-01 22:48:15 +0100360 if (!p)
361 return NULL;
362 p++;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200363 len -= (p - params);
364 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100365 }
366 return NULL;
367}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200368
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200369
Willy Tarreaubaaee002006-06-26 02:48:02 +0200370/*
Benoitaffb4812009-03-25 13:02:10 +0100371 * This function tries to find a running server for the proxy <px> following
372 * the Header parameter hash method. It looks for a specific parameter in the
373 * URL and hashes it to compute the server ID. This is useful to optimize
374 * performance by avoiding bounces between servers in contexts where sessions
375 * are shared but cookies are not usable. If the parameter is not found, NULL
376 * is returned. If any server is found, it will be returned. If no valid server
377 * is found, NULL is returned.
378 */
379struct server *get_server_hh(struct session *s)
380{
Dan Dubovik82456752014-07-08 08:51:03 -0700381 unsigned int hash = 0;
Benoitaffb4812009-03-25 13:02:10 +0100382 struct http_txn *txn = &s->txn;
Benoitaffb4812009-03-25 13:02:10 +0100383 struct proxy *px = s->be;
384 unsigned int plen = px->hh_len;
385 unsigned long len;
386 struct hdr_ctx ctx;
387 const char *p;
Bhaskar98634f02013-10-29 23:30:51 -0400388 const char *start, *end;
Benoitaffb4812009-03-25 13:02:10 +0100389
390 /* tot_weight appears to mean srv_count */
391 if (px->lbprm.tot_weight == 0)
392 return NULL;
393
Benoitaffb4812009-03-25 13:02:10 +0100394 ctx.idx = 0;
395
396 /* if the message is chunked, we skip the chunk size, but use the value as len */
Willy Tarreau211cdec2014-04-17 20:18:08 +0200397 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 +0100398
399 /* if the header is not found or empty, let's fallback to round robin */
400 if (!ctx.idx || !ctx.vlen)
401 return NULL;
402
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200403 /* note: we won't hash if there's only one server left */
404 if (px->lbprm.tot_used == 1)
405 goto hash_done;
406
Benoitaffb4812009-03-25 13:02:10 +0100407 /* Found a the hh_name in the headers.
408 * we will compute the hash based on this value ctx.val.
409 */
410 len = ctx.vlen;
411 p = (char *)ctx.line + ctx.val;
412 if (!px->hh_match_domain) {
Bhaskar98634f02013-10-29 23:30:51 -0400413 hash = gen_hash(px, p, len);
Benoitaffb4812009-03-25 13:02:10 +0100414 } else {
415 int dohash = 0;
Cyril Bonté7ccea262015-01-04 15:17:36 +0100416 p += len;
Benoitaffb4812009-03-25 13:02:10 +0100417 /* special computation, use only main domain name, not tld/host
418 * going back from the end of string, start hashing at first
419 * dot stop at next.
420 * This is designed to work with the 'Host' header, and requires
421 * a special option to activate this.
422 */
Cyril Bonté7ccea262015-01-04 15:17:36 +0100423 end = p;
Benoitaffb4812009-03-25 13:02:10 +0100424 while (len) {
Cyril Bonté7ccea262015-01-04 15:17:36 +0100425 if (dohash) {
426 /* Rewind the pointer until the previous char
427 * is a dot, this will allow to set the start
428 * position of the domain. */
429 if (*(p - 1) == '.')
Benoitaffb4812009-03-25 13:02:10 +0100430 break;
Benoitaffb4812009-03-25 13:02:10 +0100431 }
Cyril Bonté7ccea262015-01-04 15:17:36 +0100432 else if (*p == '.') {
433 /* The pointer is rewinded to the dot before the
434 * tld, we memorize the end of the domain and
435 * can enter the domain processing. */
436 end = p;
437 dohash = 1;
438 }
Benoitaffb4812009-03-25 13:02:10 +0100439 p--;
Cyril Bonté7ccea262015-01-04 15:17:36 +0100440 len--;
Benoitaffb4812009-03-25 13:02:10 +0100441 }
Cyril Bonté7ccea262015-01-04 15:17:36 +0100442 start = p;
Bhaskar98634f02013-10-29 23:30:51 -0400443 hash = gen_hash(px, start, (end - start));
Benoitaffb4812009-03-25 13:02:10 +0100444 }
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500445 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100446 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200447 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200448 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
449 return chash_get_server_hash(px, hash);
450 else
451 return map_get_server_hash(px, hash);
Benoitaffb4812009-03-25 13:02:10 +0100452}
453
Willy Tarreau34db1082012-04-19 17:16:54 +0200454/* RDP Cookie HASH. */
Emeric Brun736aa232009-06-30 17:56:00 +0200455struct server *get_server_rch(struct session *s)
456{
Dan Dubovik82456752014-07-08 08:51:03 -0700457 unsigned int hash = 0;
Emeric Brun736aa232009-06-30 17:56:00 +0200458 struct proxy *px = s->be;
459 unsigned long len;
Emeric Brun736aa232009-06-30 17:56:00 +0200460 int ret;
Willy Tarreau37406352012-04-23 16:16:37 +0200461 struct sample smp;
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200462 int rewind;
Emeric Brun736aa232009-06-30 17:56:00 +0200463
464 /* tot_weight appears to mean srv_count */
465 if (px->lbprm.tot_weight == 0)
466 return NULL;
467
Willy Tarreau37406352012-04-23 16:16:37 +0200468 memset(&smp, 0, sizeof(smp));
Emeric Brun736aa232009-06-30 17:56:00 +0200469
Willy Tarreau9b28e032012-10-12 23:49:43 +0200470 b_rew(s->req->buf, rewind = s->req->buf->o);
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200471
Willy Tarreaucadd8c92013-07-22 18:09:52 +0200472 ret = fetch_rdp_cookie_name(s, &smp, px->hh_name, px->hh_len);
Willy Tarreauf853c462012-04-23 18:53:56 +0200473 len = smp.data.str.len;
Willy Tarreau664092c2011-12-16 19:11:42 +0100474
Willy Tarreau9b28e032012-10-12 23:49:43 +0200475 b_adv(s->req->buf, rewind);
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200476
Willy Tarreau37406352012-04-23 16:16:37 +0200477 if (ret == 0 || (smp.flags & SMP_F_MAY_CHANGE) || len == 0)
Emeric Brun736aa232009-06-30 17:56:00 +0200478 return NULL;
479
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200480 /* note: we won't hash if there's only one server left */
481 if (px->lbprm.tot_used == 1)
482 goto hash_done;
483
Emeric Brun736aa232009-06-30 17:56:00 +0200484 /* Found a the hh_name in the headers.
485 * we will compute the hash based on this value ctx.val.
486 */
Bhaskar98634f02013-10-29 23:30:51 -0400487 hash = gen_hash(px, smp.data.str.str, len);
488
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500489 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100490 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200491 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200492 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
493 return chash_get_server_hash(px, hash);
494 else
495 return map_get_server_hash(px, hash);
Emeric Brun736aa232009-06-30 17:56:00 +0200496}
Benoitaffb4812009-03-25 13:02:10 +0100497
498/*
Willy Tarreau7c669d72008-06-20 15:04:11 +0200499 * This function applies the load-balancing algorithm to the session, as
500 * defined by the backend it is assigned to. The session is then marked as
501 * 'assigned'.
502 *
503 * This function MAY NOT be called with SN_ASSIGNED already set. If the session
504 * had a server previously assigned, it is rebalanced, trying to avoid the same
Willy Tarreau827aee92011-03-10 16:55:02 +0100505 * server, which should still be present in target_srv(&s->target) before the call.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200506 * The function tries to keep the original connection slot if it reconnects to
507 * the same server, otherwise it releases it and tries to offer it.
508 *
509 * It is illegal to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200510 *
511 * It may return :
Willy Tarreau664beb82011-03-10 11:38:29 +0100512 * SRV_STATUS_OK if everything is OK. ->srv and ->target are assigned.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200513 * SRV_STATUS_NOSRV if no server is available. Session is not ASSIGNED
514 * SRV_STATUS_FULL if all servers are saturated. Session is not ASSIGNED
Willy Tarreaubaaee002006-06-26 02:48:02 +0200515 * SRV_STATUS_INTERNAL for other unrecoverable errors.
516 *
Willy Tarreau7c669d72008-06-20 15:04:11 +0200517 * Upon successful return, the session flag SN_ASSIGNED is set to indicate that
Willy Tarreau827aee92011-03-10 16:55:02 +0100518 * it does not need to be called anymore. This means that target_srv(&s->target)
519 * can be trusted in balance and direct modes.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200520 *
521 */
522
523int assign_server(struct session *s)
524{
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200525 struct connection *conn;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200526 struct server *conn_slot;
Willy Tarreau827aee92011-03-10 16:55:02 +0100527 struct server *srv, *prev_srv;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200528 int err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100529
Simon Horman8effd3d2011-08-13 08:03:52 +0900530 DPRINTF(stderr,"assign_server : s=%p\n",s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200531
Willy Tarreau7c669d72008-06-20 15:04:11 +0200532 err = SRV_STATUS_INTERNAL;
533 if (unlikely(s->pend_pos || s->flags & SN_ASSIGNED))
534 goto out_err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100535
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100536 prev_srv = objt_server(s->target);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200537 conn_slot = s->srv_conn;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200538
Willy Tarreau7c669d72008-06-20 15:04:11 +0200539 /* We have to release any connection slot before applying any LB algo,
540 * otherwise we may erroneously end up with no available slot.
541 */
542 if (conn_slot)
543 sess_change_server(s, NULL);
544
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100545 /* We will now try to find the good server and store it into <objt_server(s->target)>.
546 * Note that <objt_server(s->target)> may be NULL in case of dispatch or proxy mode,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200547 * as well as if no server is available (check error code).
548 */
Willy Tarreau1a20a5d2007-11-01 21:08:19 +0100549
Willy Tarreau827aee92011-03-10 16:55:02 +0100550 srv = NULL;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100551 s->target = NULL;
Willy Tarreau9420b122013-12-15 18:58:25 +0100552 conn = objt_conn(s->req->cons->end);
Willy Tarreau664beb82011-03-10 11:38:29 +0100553
Willy Tarreau068621e2013-12-23 15:11:25 +0100554 if (conn &&
Willy Tarreau2481d162014-02-19 18:40:43 +0100555 (conn->flags & CO_FL_CONNECTED) &&
Willy Tarreau9420b122013-12-15 18:58:25 +0100556 objt_server(conn->target) && __objt_server(conn->target)->proxy == s->be &&
Willy Tarreauc35362a2014-04-25 13:58:37 +0200557 ((s->txn.flags & TX_PREFER_LAST) ||
558 ((s->be->options & PR_O_PREF_LAST) &&
559 (!s->be->max_ka_queue ||
560 server_has_room(__objt_server(conn->target)) ||
561 (__objt_server(conn->target)->nbpend + 1) < s->be->max_ka_queue))) &&
Willy Tarreau87eb1d62014-05-13 18:51:40 +0200562 srv_is_usable(__objt_server(conn->target))) {
Willy Tarreau9420b122013-12-15 18:58:25 +0100563 /* This session was relying on a server in a previous request
Godbachcfbb93f2014-12-10 10:21:30 +0800564 * and the proxy has "option prefer-last-server" set, so
Willy Tarreau9420b122013-12-15 18:58:25 +0100565 * let's try to reuse the same server.
566 */
567 srv = __objt_server(conn->target);
568 s->target = &srv->obj_type;
569 }
570 else if (s->be->lbprm.algo & BE_LB_KIND) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200571 /* we must check if we have at least one server available */
572 if (!s->be->lbprm.tot_weight) {
573 err = SRV_STATUS_NOSRV;
574 goto out;
575 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200576
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200577 /* First check whether we need to fetch some data or simply call
578 * the LB lookup function. Only the hashing functions will need
579 * some input data in fact, and will support multiple algorithms.
580 */
581 switch (s->be->lbprm.algo & BE_LB_LKUP) {
582 case BE_LB_LKUP_RRTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100583 srv = fwrr_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200584 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200585
Willy Tarreauf09c6602012-02-13 17:12:08 +0100586 case BE_LB_LKUP_FSTREE:
587 srv = fas_get_next_server(s->be, prev_srv);
588 break;
589
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200590 case BE_LB_LKUP_LCTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100591 srv = fwlc_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200592 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200593
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200594 case BE_LB_LKUP_CHTREE:
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200595 case BE_LB_LKUP_MAP:
Willy Tarreau9757a382009-10-03 12:56:50 +0200596 if ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200597 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100598 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200599 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100600 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau9757a382009-10-03 12:56:50 +0200601 break;
602 }
603 else if ((s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_HI) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200604 /* unknown balancing algorithm */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200605 err = SRV_STATUS_INTERNAL;
606 goto out;
607 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200608
609 switch (s->be->lbprm.algo & BE_LB_PARM) {
610 case BE_LB_HASH_SRC:
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200611 conn = objt_conn(s->req->prod->end);
612 if (conn && conn->addr.from.ss_family == AF_INET) {
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200613 srv = get_server_sh(s->be,
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200614 (void *)&((struct sockaddr_in *)&conn->addr.from)->sin_addr,
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200615 4);
616 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200617 else if (conn && conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200618 srv = get_server_sh(s->be,
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200619 (void *)&((struct sockaddr_in6 *)&conn->addr.from)->sin6_addr,
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200620 16);
621 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200622 else {
623 /* unknown IP family */
624 err = SRV_STATUS_INTERNAL;
625 goto out;
626 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200627 break;
628
629 case BE_LB_HASH_URI:
630 /* URI 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_uh(s->be,
Willy Tarreauda6eed62014-04-17 20:24:24 +0200634 b_ptr(s->req->buf, -http_uri_rewind(&s->txn.req)),
Willy Tarreau827aee92011-03-10 16:55:02 +0100635 s->txn.req.sl.rq.u_l);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200636 break;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200637
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200638 case BE_LB_HASH_PRM:
639 /* URL Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100640 if (s->txn.req.msg_state < HTTP_MSG_BODY)
641 break;
Willy Tarreau61a21a32011-03-01 20:35:49 +0100642
Willy Tarreau827aee92011-03-10 16:55:02 +0100643 srv = get_server_ph(s->be,
Willy Tarreauda6eed62014-04-17 20:24:24 +0200644 b_ptr(s->req->buf, -http_uri_rewind(&s->txn.req)),
Willy Tarreau827aee92011-03-10 16:55:02 +0100645 s->txn.req.sl.rq.u_l);
Willy Tarreau61a21a32011-03-01 20:35:49 +0100646
Willy Tarreau827aee92011-03-10 16:55:02 +0100647 if (!srv && s->txn.meth == HTTP_METH_POST)
648 srv = get_server_ph_post(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200649 break;
Benoitaffb4812009-03-25 13:02:10 +0100650
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200651 case BE_LB_HASH_HDR:
652 /* Header Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100653 if (s->txn.req.msg_state < HTTP_MSG_BODY)
654 break;
Willy Tarreau827aee92011-03-10 16:55:02 +0100655 srv = get_server_hh(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200656 break;
657
658 case BE_LB_HASH_RDP:
659 /* RDP Cookie hashing */
Willy Tarreau827aee92011-03-10 16:55:02 +0100660 srv = get_server_rch(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200661 break;
662
663 default:
664 /* unknown balancing algorithm */
665 err = SRV_STATUS_INTERNAL;
666 goto out;
Benoitaffb4812009-03-25 13:02:10 +0100667 }
Emeric Brun736aa232009-06-30 17:56:00 +0200668
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200669 /* If the hashing parameter was not found, let's fall
670 * back to round robin on the map.
671 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100672 if (!srv) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200673 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100674 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200675 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100676 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200677 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200678
679 /* end of map-based LB */
Emeric Brun736aa232009-06-30 17:56:00 +0200680 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200681
Willy Tarreau7c669d72008-06-20 15:04:11 +0200682 default:
683 /* unknown balancing algorithm */
684 err = SRV_STATUS_INTERNAL;
685 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200686 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200687
Willy Tarreau827aee92011-03-10 16:55:02 +0100688 if (!srv) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200689 err = SRV_STATUS_FULL;
690 goto out;
691 }
Willy Tarreau827aee92011-03-10 16:55:02 +0100692 else if (srv != prev_srv) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100693 s->be->be_counters.cum_lbconn++;
Willy Tarreau827aee92011-03-10 16:55:02 +0100694 srv->counters.cum_lbconn++;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100695 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100696 s->target = &srv->obj_type;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200697 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200698 else if (s->be->options & (PR_O_DISPATCH | PR_O_TRANSP)) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100699 s->target = &s->be->obj_type;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200700 }
David du Colombier6f5ccb12011-03-10 22:26:24 +0100701 else if ((s->be->options & PR_O_HTTP_PROXY) &&
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200702 (conn = objt_conn(s->req->cons->end)) &&
703 is_addr(&conn->addr.to)) {
Willy Tarreau664beb82011-03-10 11:38:29 +0100704 /* in proxy mode, we need a valid destination address */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100705 s->target = &s->be->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +0100706 }
707 else {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200708 err = SRV_STATUS_NOSRV;
709 goto out;
710 }
711
712 s->flags |= SN_ASSIGNED;
713 err = SRV_STATUS_OK;
714 out:
715
716 /* Either we take back our connection slot, or we offer it to someone
717 * else if we don't need it anymore.
718 */
719 if (conn_slot) {
Willy Tarreau827aee92011-03-10 16:55:02 +0100720 if (conn_slot == srv) {
721 sess_change_server(s, srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200722 } else {
723 if (may_dequeue_tasks(conn_slot, s->be))
724 process_srv_queue(conn_slot);
725 }
726 }
727
728 out_err:
729 return err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200730}
731
732
733/*
734 * This function assigns a server address to a session, and sets SN_ADDR_SET.
735 * The address is taken from the currently assigned server, or from the
736 * dispatch or transparent address.
737 *
738 * It may return :
739 * SRV_STATUS_OK if everything is OK.
740 * SRV_STATUS_INTERNAL for other unrecoverable errors.
741 *
742 * Upon successful return, the session flag SN_ADDR_SET is set. This flag is
743 * not cleared, so it's to the caller to clear it if required.
744 *
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200745 * The caller is responsible for having already assigned a connection
746 * to si->end.
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200747 *
Willy Tarreaubaaee002006-06-26 02:48:02 +0200748 */
749int assign_server_address(struct session *s)
750{
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200751 struct connection *cli_conn = objt_conn(s->req->prod->end);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200752 struct connection *srv_conn = objt_conn(s->req->cons->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200753
Willy Tarreaubaaee002006-06-26 02:48:02 +0200754#ifdef DEBUG_FULL
755 fprintf(stderr,"assign_server_address : s=%p\n",s);
756#endif
757
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200758 if ((s->flags & SN_DIRECT) || (s->be->lbprm.algo & BE_LB_KIND)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200759 /* A server is necessarily known for this session */
760 if (!(s->flags & SN_ASSIGNED))
761 return SRV_STATUS_INTERNAL;
762
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200763 srv_conn->addr.to = objt_server(s->target)->addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200764
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200765 if (!is_addr(&srv_conn->addr.to) && cli_conn) {
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200766 /* if the server has no address, we use the same address
767 * the client asked, which is handy for remapping ports
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200768 * locally on multiple addresses at once. Nothing is done
769 * for AF_UNIX addresses.
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200770 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200771 conn_get_to_addr(cli_conn);
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200772
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200773 if (cli_conn->addr.to.ss_family == AF_INET) {
774 ((struct sockaddr_in *)&srv_conn->addr.to)->sin_addr = ((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
775 } else if (cli_conn->addr.to.ss_family == AF_INET6) {
776 ((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 +0200777 }
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200778 }
779
Willy Tarreaubaaee002006-06-26 02:48:02 +0200780 /* if this server remaps proxied ports, we'll use
781 * the port the client connected to with an offset. */
Willy Tarreauc93cd162014-05-13 15:54:22 +0200782 if ((objt_server(s->target)->flags & SRV_F_MAPPORTS) && cli_conn) {
David du Colombier6f5ccb12011-03-10 22:26:24 +0100783 int base_port;
784
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200785 conn_get_to_addr(cli_conn);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100786
787 /* First, retrieve the port from the incoming connection */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200788 base_port = get_host_port(&cli_conn->addr.to);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100789
790 /* Second, assign the outgoing connection's port */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200791 base_port += get_host_port(&srv_conn->addr.to);
792 set_host_port(&srv_conn->addr.to, base_port);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200793 }
794 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200795 else if (s->be->options & PR_O_DISPATCH) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200796 /* connect to the defined dispatch addr */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200797 srv_conn->addr.to = s->be->dispatch_addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200798 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200799 else if ((s->be->options & PR_O_TRANSP) && cli_conn) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200800 /* in transparent mode, use the original dest addr if no dispatch specified */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200801 conn_get_to_addr(cli_conn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200802
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200803 if (cli_conn->addr.to.ss_family == AF_INET || cli_conn->addr.to.ss_family == AF_INET6)
804 srv_conn->addr.to = cli_conn->addr.to;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200805 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100806 else if (s->be->options & PR_O_HTTP_PROXY) {
807 /* If HTTP PROXY option is set, then server is already assigned
808 * during incoming client request parsing. */
809 }
Willy Tarreau1a1158b2007-01-20 11:07:46 +0100810 else {
811 /* no server and no LB algorithm ! */
812 return SRV_STATUS_INTERNAL;
813 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200814
815 s->flags |= SN_ADDR_SET;
816 return SRV_STATUS_OK;
817}
818
819
820/* This function assigns a server to session <s> if required, and can add the
821 * connection to either the assigned server's queue or to the proxy's queue.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200822 * If ->srv_conn is set, the session is first released from the server.
823 * It may also be called with SN_DIRECT and/or SN_ASSIGNED though. It will
824 * be called before any connection and after any retry or redispatch occurs.
825 *
826 * It is not allowed to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200827 *
828 * Returns :
829 *
830 * SRV_STATUS_OK if everything is OK.
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100831 * SRV_STATUS_NOSRV if no server is available. objt_server(s->target) = NULL.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200832 * SRV_STATUS_QUEUED if the connection has been queued.
833 * SRV_STATUS_FULL if the server(s) is/are saturated and the
Willy Tarreau827aee92011-03-10 16:55:02 +0100834 * connection could not be queued at the server's,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200835 * which may be NULL if we queue on the backend.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200836 * SRV_STATUS_INTERNAL for other unrecoverable errors.
837 *
838 */
839int assign_server_and_queue(struct session *s)
840{
841 struct pendconn *p;
Willy Tarreau827aee92011-03-10 16:55:02 +0100842 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200843 int err;
844
845 if (s->pend_pos)
846 return SRV_STATUS_INTERNAL;
847
Willy Tarreau7c669d72008-06-20 15:04:11 +0200848 err = SRV_STATUS_OK;
849 if (!(s->flags & SN_ASSIGNED)) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100850 struct server *prev_srv = objt_server(s->target);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100851
Willy Tarreau7c669d72008-06-20 15:04:11 +0200852 err = assign_server(s);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100853 if (prev_srv) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200854 /* This session was previously assigned to a server. We have to
855 * update the session's and the server's stats :
856 * - if the server changed :
857 * - set TX_CK_DOWN if txn.flags was TX_CK_VALID
858 * - set SN_REDISP if it was successfully redispatched
859 * - increment srv->redispatches and be->redispatches
860 * - if the server remained the same : update retries.
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100861 */
862
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100863 if (prev_srv != objt_server(s->target)) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200864 if ((s->txn.flags & TX_CK_MASK) == TX_CK_VALID) {
865 s->txn.flags &= ~TX_CK_MASK;
866 s->txn.flags |= TX_CK_DOWN;
867 }
868 s->flags |= SN_REDISP;
Willy Tarreau3d80d912011-03-10 11:42:13 +0100869 prev_srv->counters.redispatches++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100870 s->be->be_counters.redispatches++;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200871 } else {
Willy Tarreau3d80d912011-03-10 11:42:13 +0100872 prev_srv->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100873 s->be->be_counters.retries++;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100874 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100875 }
876 }
877
Willy Tarreaubaaee002006-06-26 02:48:02 +0200878 switch (err) {
879 case SRV_STATUS_OK:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200880 /* we have SN_ASSIGNED set */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100881 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100882 if (!srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200883 return SRV_STATUS_OK; /* dispatch or proxy mode */
884
885 /* If we already have a connection slot, no need to check any queue */
Willy Tarreau827aee92011-03-10 16:55:02 +0100886 if (s->srv_conn == srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200887 return SRV_STATUS_OK;
888
889 /* OK, this session already has an assigned server, but no
890 * connection slot yet. Either it is a redispatch, or it was
891 * assigned from persistence information (direct mode).
892 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100893 if ((s->flags & SN_REDIRECTABLE) && srv->rdr_len) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200894 /* server scheduled for redirection, and already assigned. We
895 * don't want to go further nor check the queue.
Willy Tarreau21d2af32008-02-14 20:25:24 +0100896 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100897 sess_change_server(s, srv); /* not really needed in fact */
Willy Tarreau21d2af32008-02-14 20:25:24 +0100898 return SRV_STATUS_OK;
899 }
900
Willy Tarreau7c669d72008-06-20 15:04:11 +0200901 /* We might have to queue this session if the assigned server is full.
902 * We know we have to queue it into the server's queue, so if a maxqueue
903 * is set on the server, we must also check that the server's queue is
904 * not full, in which case we have to return FULL.
905 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100906 if (srv->maxconn &&
907 (srv->nbpend || srv->served >= srv_dynamic_maxconn(srv))) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200908
Willy Tarreau827aee92011-03-10 16:55:02 +0100909 if (srv->maxqueue > 0 && srv->nbpend >= srv->maxqueue)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200910 return SRV_STATUS_FULL;
911
Willy Tarreaubaaee002006-06-26 02:48:02 +0200912 p = pendconn_add(s);
913 if (p)
914 return SRV_STATUS_QUEUED;
915 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200916 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200917 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200918
919 /* OK, we can use this server. Let's reserve our place */
Willy Tarreau827aee92011-03-10 16:55:02 +0100920 sess_change_server(s, srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200921 return SRV_STATUS_OK;
922
923 case SRV_STATUS_FULL:
924 /* queue this session into the proxy's queue */
925 p = pendconn_add(s);
926 if (p)
927 return SRV_STATUS_QUEUED;
928 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200929 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200930
931 case SRV_STATUS_NOSRV:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200932 return err;
933
Willy Tarreaubaaee002006-06-26 02:48:02 +0200934 case SRV_STATUS_INTERNAL:
935 return err;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200936
Willy Tarreaubaaee002006-06-26 02:48:02 +0200937 default:
938 return SRV_STATUS_INTERNAL;
939 }
Willy Tarreau5b6995c2008-01-13 16:31:17 +0100940}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200941
Willy Tarreaub1d67742010-03-29 19:36:59 +0200942/* If an explicit source binding is specified on the server and/or backend, and
943 * this source makes use of the transparent proxy, then it is extracted now and
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200944 * assigned to the session's pending connection. This function assumes that an
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200945 * outgoing connection has already been assigned to s->req->cons->end.
Willy Tarreaub1d67742010-03-29 19:36:59 +0200946 */
947static void assign_tproxy_address(struct session *s)
948{
Pieter Baauwd551fb52013-05-08 22:49:23 +0200949#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_TRANSPARENT)
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100950 struct server *srv = objt_server(s->target);
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100951 struct conn_src *src;
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200952 struct connection *cli_conn;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200953 struct connection *srv_conn = objt_conn(s->req->cons->end);
Willy Tarreau827aee92011-03-10 16:55:02 +0100954
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100955 if (srv && srv->conn_src.opts & CO_SRC_BIND)
956 src = &srv->conn_src;
957 else if (s->be->conn_src.opts & CO_SRC_BIND)
958 src = &s->be->conn_src;
959 else
960 return;
Willy Tarreau294c4732011-12-16 21:35:50 +0100961
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100962 switch (src->opts & CO_SRC_TPROXY_MASK) {
963 case CO_SRC_TPROXY_ADDR:
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200964 srv_conn->addr.from = src->tproxy_addr;
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100965 break;
966 case CO_SRC_TPROXY_CLI:
967 case CO_SRC_TPROXY_CIP:
968 /* FIXME: what can we do if the client connects in IPv6 or unix socket ? */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200969 cli_conn = objt_conn(s->req->prod->end);
970 if (cli_conn)
971 srv_conn->addr.from = cli_conn->addr.from;
972 else
973 memset(&srv_conn->addr.from, 0, sizeof(srv_conn->addr.from));
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100974 break;
975 case CO_SRC_TPROXY_DYN:
976 if (src->bind_hdr_occ) {
977 char *vptr;
978 int vlen;
979 int rewind;
Willy Tarreau294c4732011-12-16 21:35:50 +0100980
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100981 /* bind to the IP in a header */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200982 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_family = AF_INET;
983 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_port = 0;
984 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_addr.s_addr = 0;
Willy Tarreau294c4732011-12-16 21:35:50 +0100985
Willy Tarreau211cdec2014-04-17 20:18:08 +0200986 b_rew(s->req->buf, rewind = http_hdr_rewind(&s->txn.req));
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100987 if (http_get_hdr(&s->txn.req, src->bind_hdr_name, src->bind_hdr_len,
988 &s->txn.hdr_idx, src->bind_hdr_occ, NULL, &vptr, &vlen)) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200989 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_addr.s_addr =
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100990 htonl(inetaddr_host_lim(vptr, vptr + vlen));
Willy Tarreaubce70882009-09-07 11:51:47 +0200991 }
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100992 b_adv(s->req->buf, rewind);
Willy Tarreaub1d67742010-03-29 19:36:59 +0200993 }
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100994 break;
995 default:
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200996 memset(&srv_conn->addr.from, 0, sizeof(srv_conn->addr.from));
Willy Tarreaub1d67742010-03-29 19:36:59 +0200997 }
998#endif
999}
1000
1001
Willy Tarreaubaaee002006-06-26 02:48:02 +02001002/*
1003 * This function initiates a connection to the server assigned to this session
Willy Tarreau6471afb2011-09-23 10:54:59 +02001004 * (s->target, s->req->cons->addr.to). It will assign a server if none
Willy Tarreau664beb82011-03-10 11:38:29 +01001005 * is assigned yet.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001006 * It can return one of :
1007 * - SN_ERR_NONE if everything's OK
1008 * - SN_ERR_SRVTO if there are no more servers
1009 * - SN_ERR_SRVCL if the connection was refused by the server
1010 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1011 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1012 * - SN_ERR_INTERNAL for any other purely internal errors
1013 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001014 * The server-facing stream interface is expected to hold a pre-allocated connection
1015 * in s->req->cons->conn.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001016 */
1017int connect_server(struct session *s)
1018{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001019 struct connection *cli_conn;
Willy Tarreau34601a82013-12-15 16:33:46 +01001020 struct connection *srv_conn;
Willy Tarreau827aee92011-03-10 16:55:02 +01001021 struct server *srv;
Willy Tarreau34601a82013-12-15 16:33:46 +01001022 int reuse = 0;
Willy Tarreau9650f372009-08-16 14:02:45 +02001023 int err;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001024
Willy Tarreau34601a82013-12-15 16:33:46 +01001025 srv_conn = objt_conn(s->req->cons->end);
1026 if (srv_conn)
1027 reuse = s->target == srv_conn->target;
1028
1029 if (reuse) {
1030 /* Disable connection reuse if a dynamic source is used.
1031 * As long as we don't share connections between servers,
1032 * we don't need to disable connection reuse on no-idempotent
1033 * requests nor when PROXY protocol is used.
1034 */
1035 srv = objt_server(s->target);
1036 if (srv && srv->conn_src.opts & CO_SRC_BIND) {
1037 if ((srv->conn_src.opts & CO_SRC_TPROXY_MASK) == CO_SRC_TPROXY_DYN)
1038 reuse = 0;
1039 }
1040 else if (s->be->conn_src.opts & CO_SRC_BIND) {
1041 if ((s->be->conn_src.opts & CO_SRC_TPROXY_MASK) == CO_SRC_TPROXY_DYN)
1042 reuse = 0;
1043 }
1044 }
1045
1046 srv_conn = si_alloc_conn(s->req->cons, reuse);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001047 if (!srv_conn)
1048 return SN_ERR_RESOURCE;
1049
Willy Tarreaubaaee002006-06-26 02:48:02 +02001050 if (!(s->flags & SN_ADDR_SET)) {
1051 err = assign_server_address(s);
1052 if (err != SRV_STATUS_OK)
1053 return SN_ERR_INTERNAL;
1054 }
1055
Willy Tarreauff605db2013-12-20 11:09:51 +01001056 if (!conn_xprt_ready(srv_conn)) {
1057 /* the target was only on the session, assign it to the SI now */
1058 srv_conn->target = s->target;
Willy Tarreaud88edf22009-06-14 15:48:17 +02001059
Willy Tarreauff605db2013-12-20 11:09:51 +01001060 /* set the correct protocol on the output stream interface */
1061 if (objt_server(s->target)) {
1062 conn_prepare(srv_conn, objt_server(s->target)->proto, objt_server(s->target)->xprt);
1063 }
1064 else if (obj_type(s->target) == OBJ_TYPE_PROXY) {
1065 /* proxies exclusively run on raw_sock right now */
1066 conn_prepare(srv_conn, protocol_by_family(srv_conn->addr.to.ss_family), &raw_sock);
1067 if (!objt_conn(s->req->cons->end) || !objt_conn(s->req->cons->end)->ctrl)
1068 return SN_ERR_INTERNAL;
1069 }
1070 else
1071 return SN_ERR_INTERNAL; /* how did we get there ? */
Willy Tarreaud02394b2012-05-11 18:32:18 +02001072
Willy Tarreauff605db2013-12-20 11:09:51 +01001073 /* process the case where the server requires the PROXY protocol to be sent */
1074 srv_conn->send_proxy_ofs = 0;
David Safb76832014-05-08 23:42:08 -04001075 if (objt_server(s->target) && objt_server(s->target)->pp_opts) {
Willy Tarreauff605db2013-12-20 11:09:51 +01001076 srv_conn->send_proxy_ofs = 1; /* must compute size */
1077 cli_conn = objt_conn(s->req->prod->end);
1078 if (cli_conn)
1079 conn_get_to_addr(cli_conn);
1080 }
Willy Tarreau2a6e8802013-10-24 15:50:53 +02001081
Willy Tarreauff605db2013-12-20 11:09:51 +01001082 si_attach_conn(s->req->cons, srv_conn);
Willy Tarreau26d8c592012-05-07 18:12:14 +02001083
Willy Tarreauff605db2013-12-20 11:09:51 +01001084 assign_tproxy_address(s);
1085 }
1086 else {
1087 /* the connection is being reused, just re-attach it */
1088 si_attach_conn(s->req->cons, srv_conn);
Willy Tarreau36346242014-02-24 18:26:30 +01001089 s->flags |= SN_SRV_REUSED;
Willy Tarreauff605db2013-12-20 11:09:51 +01001090 }
Willy Tarreaub1d67742010-03-29 19:36:59 +02001091
William Lallemandb7ff6a32012-03-02 14:35:21 +01001092 /* flag for logging source ip/port */
1093 if (s->fe->options2 & PR_O2_SRC_ADDR)
1094 s->req->cons->flags |= SI_FL_SRC_ADDR;
1095
Willy Tarreau14f8e862012-08-30 22:23:13 +02001096 /* disable lingering */
1097 if (s->be->options & PR_O_TCP_NOLING)
1098 s->req->cons->flags |= SI_FL_NOLINGER;
1099
Willy Tarreau73b013b2012-05-21 16:31:45 +02001100 err = si_connect(s->req->cons);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001101
Willy Tarreau9650f372009-08-16 14:02:45 +02001102 if (err != SN_ERR_NONE)
1103 return err;
Willy Tarreau788e2842008-08-26 13:25:39 +02001104
Willy Tarreau14f8e862012-08-30 22:23:13 +02001105 /* set connect timeout */
1106 s->req->cons->exp = tick_add_ifset(now_ms, s->be->timeout.connect);
1107
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001108 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001109 if (srv) {
Willy Tarreau1e62de62008-11-11 20:20:02 +01001110 s->flags |= SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001111 srv->cur_sess++;
1112 if (srv->cur_sess > srv->counters.cur_sess_max)
1113 srv->counters.cur_sess_max = srv->cur_sess;
Willy Tarreau51406232008-03-10 22:04:20 +01001114 if (s->be->lbprm.server_take_conn)
Willy Tarreau827aee92011-03-10 16:55:02 +01001115 s->be->lbprm.server_take_conn(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001116 }
1117
Willy Tarreaubaaee002006-06-26 02:48:02 +02001118 return SN_ERR_NONE; /* connection is OK */
1119}
1120
1121
Willy Tarreaubaaee002006-06-26 02:48:02 +02001122/* This function performs the "redispatch" part of a connection attempt. It
1123 * will assign a server if required, queue the connection if required, and
1124 * handle errors that might arise at this level. It can change the server
1125 * state. It will return 1 if it encounters an error, switches the server
1126 * state, or has to queue a connection. Otherwise, it will return 0 indicating
1127 * that the connection is ready to use.
1128 */
1129
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001130int srv_redispatch_connect(struct session *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001131{
Willy Tarreau827aee92011-03-10 16:55:02 +01001132 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001133 int conn_err;
1134
1135 /* We know that we don't have any connection pending, so we will
1136 * try to get a new one, and wait in this state if it's queued
1137 */
Willy Tarreau7c669d72008-06-20 15:04:11 +02001138 redispatch:
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001139 conn_err = assign_server_and_queue(s);
1140 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001141
Willy Tarreaubaaee002006-06-26 02:48:02 +02001142 switch (conn_err) {
1143 case SRV_STATUS_OK:
1144 break;
1145
Willy Tarreau7c669d72008-06-20 15:04:11 +02001146 case SRV_STATUS_FULL:
1147 /* The server has reached its maxqueue limit. Either PR_O_REDISP is set
1148 * and we can redispatch to another server, or it is not and we return
1149 * 503. This only makes sense in DIRECT mode however, because normal LB
1150 * algorithms would never select such a server, and hash algorithms
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001151 * would bring us on the same server again. Note that s->target is set
Willy Tarreau827aee92011-03-10 16:55:02 +01001152 * in this case.
Willy Tarreau7c669d72008-06-20 15:04:11 +02001153 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001154 if (((s->flags & (SN_DIRECT|SN_FORCE_PRST)) == SN_DIRECT) &&
1155 (s->be->options & PR_O_REDISP)) {
1156 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Willy Tarreau7c669d72008-06-20 15:04:11 +02001157 goto redispatch;
1158 }
1159
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001160 if (!s->req->cons->err_type) {
1161 s->req->cons->err_type = SI_ET_QUEUE_ERR;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001162 }
Willy Tarreau7c669d72008-06-20 15:04:11 +02001163
Willy Tarreau827aee92011-03-10 16:55:02 +01001164 srv->counters.failed_conns++;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001165 s->be->be_counters.failed_conns++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02001166 return 1;
1167
Willy Tarreaubaaee002006-06-26 02:48:02 +02001168 case SRV_STATUS_NOSRV:
Willy Tarreau827aee92011-03-10 16:55:02 +01001169 /* note: it is guaranteed that srv == NULL here */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001170 if (!s->req->cons->err_type) {
1171 s->req->cons->err_type = SI_ET_CONN_ERR;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001172 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +01001173
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001174 s->be->be_counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001175 return 1;
1176
1177 case SRV_STATUS_QUEUED:
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001178 s->req->cons->exp = tick_add_ifset(now_ms, s->be->timeout.queue);
1179 s->req->cons->state = SI_ST_QUE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001180 /* do nothing else and do not wake any other session up */
1181 return 1;
1182
Willy Tarreaubaaee002006-06-26 02:48:02 +02001183 case SRV_STATUS_INTERNAL:
1184 default:
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001185 if (!s->req->cons->err_type) {
1186 s->req->cons->err_type = SI_ET_CONN_OTHER;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001187 }
1188
Willy Tarreau827aee92011-03-10 16:55:02 +01001189 if (srv)
1190 srv_inc_sess_ctr(srv);
1191 if (srv)
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001192 srv_set_sess_last(srv);
1193 if (srv)
Willy Tarreau827aee92011-03-10 16:55:02 +01001194 srv->counters.failed_conns++;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001195 s->be->be_counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001196
1197 /* release other sessions waiting for this server */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001198 if (may_dequeue_tasks(srv, s->be))
Willy Tarreau827aee92011-03-10 16:55:02 +01001199 process_srv_queue(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001200 return 1;
1201 }
1202 /* if we get here, it's because we got SRV_STATUS_OK, which also
1203 * means that the connection has not been queued.
1204 */
1205 return 0;
1206}
1207
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001208/* sends a log message when a backend goes down, and also sets last
1209 * change date.
1210 */
1211void set_backend_down(struct proxy *be)
1212{
1213 be->last_change = now.tv_sec;
1214 be->down_trans++;
1215
1216 Alert("%s '%s' has no server available!\n", proxy_type_str(be), be->id);
1217 send_log(be, LOG_EMERG, "%s %s has no server available!\n", proxy_type_str(be), be->id);
1218}
1219
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001220/* Apply RDP cookie persistence to the current session. For this, the function
1221 * tries to extract an RDP cookie from the request buffer, and look for the
1222 * matching server in the list. If the server is found, it is assigned to the
1223 * session. This always returns 1, and the analyser removes itself from the
1224 * list. Nothing is performed if a server was already assigned.
1225 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02001226int tcp_persist_rdp_cookie(struct session *s, struct channel *req, int an_bit)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001227{
1228 struct proxy *px = s->be;
1229 int ret;
Willy Tarreau37406352012-04-23 16:16:37 +02001230 struct sample smp;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001231 struct server *srv = px->srv;
1232 struct sockaddr_in addr;
1233 char *p;
1234
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001235 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 +02001236 now_ms, __FUNCTION__,
1237 s,
1238 req,
1239 req->rex, req->wex,
1240 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001241 req->buf->i,
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001242 req->analysers);
1243
1244 if (s->flags & SN_ASSIGNED)
1245 goto no_cookie;
1246
Willy Tarreau37406352012-04-23 16:16:37 +02001247 memset(&smp, 0, sizeof(smp));
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001248
Willy Tarreaucadd8c92013-07-22 18:09:52 +02001249 ret = fetch_rdp_cookie_name(s, &smp, s->be->rdp_cookie_name, s->be->rdp_cookie_len);
Willy Tarreauf853c462012-04-23 18:53:56 +02001250 if (ret == 0 || (smp.flags & SMP_F_MAY_CHANGE) || smp.data.str.len == 0)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001251 goto no_cookie;
1252
1253 memset(&addr, 0, sizeof(addr));
1254 addr.sin_family = AF_INET;
1255
Willy Tarreau664092c2011-12-16 19:11:42 +01001256 /* Considering an rdp cookie detected using acl, str ended with <cr><lf> and should return */
Willy Tarreauf853c462012-04-23 18:53:56 +02001257 addr.sin_addr.s_addr = strtoul(smp.data.str.str, &p, 10);
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001258 if (*p != '.')
1259 goto no_cookie;
1260 p++;
1261 addr.sin_port = (unsigned short)strtoul(p, &p, 10);
1262 if (*p != '.')
1263 goto no_cookie;
1264
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001265 s->target = NULL;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001266 while (srv) {
Willy Tarreau28e9d062014-05-09 22:47:50 +02001267 if (srv->addr.ss_family == AF_INET &&
1268 memcmp(&addr, &(srv->addr), sizeof(addr)) == 0) {
Willy Tarreau892337c2014-05-13 23:41:20 +02001269 if ((srv->state != SRV_ST_STOPPED) || (px->options & PR_O_PERSIST)) {
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001270 /* we found the server and it is usable */
1271 s->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001272 s->target = &srv->obj_type;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001273 break;
1274 }
1275 }
1276 srv = srv->next;
1277 }
1278
1279no_cookie:
1280 req->analysers &= ~an_bit;
1281 req->analyse_exp = TICK_ETERNITY;
1282 return 1;
1283}
1284
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001285int be_downtime(struct proxy *px) {
Willy Tarreaub625a082007-11-26 01:15:43 +01001286 if (px->lbprm.tot_weight && px->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001287 return px->down_time;
1288
1289 return now.tv_sec - px->last_change + px->down_time;
1290}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001291
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001292/*
1293 * This function returns a string containing the balancing
1294 * mode of the proxy in a format suitable for stats.
1295 */
1296
1297const char *backend_lb_algo_str(int algo) {
1298
1299 if (algo == BE_LB_ALGO_RR)
1300 return "roundrobin";
1301 else if (algo == BE_LB_ALGO_SRR)
1302 return "static-rr";
Willy Tarreauf09c6602012-02-13 17:12:08 +01001303 else if (algo == BE_LB_ALGO_FAS)
1304 return "first";
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001305 else if (algo == BE_LB_ALGO_LC)
1306 return "leastconn";
1307 else if (algo == BE_LB_ALGO_SH)
1308 return "source";
1309 else if (algo == BE_LB_ALGO_UH)
1310 return "uri";
1311 else if (algo == BE_LB_ALGO_PH)
1312 return "url_param";
1313 else if (algo == BE_LB_ALGO_HH)
1314 return "hdr";
1315 else if (algo == BE_LB_ALGO_RCH)
1316 return "rdp-cookie";
1317 else
1318 return NULL;
1319}
1320
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001321/* This function parses a "balance" statement in a backend section describing
1322 * <curproxy>. It returns -1 if there is any error, otherwise zero. If it
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001323 * returns -1, it will write an error message into the <err> buffer which will
1324 * automatically be allocated and must be passed as NULL. The trailing '\n'
1325 * will not be written. The function must be called with <args> pointing to the
1326 * first word after "balance".
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001327 */
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001328int backend_parse_balance(const char **args, char **err, struct proxy *curproxy)
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001329{
1330 if (!*(args[0])) {
1331 /* if no option is set, use round-robin by default */
Willy Tarreau31682232007-11-29 15:38:04 +01001332 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1333 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001334 return 0;
1335 }
1336
1337 if (!strcmp(args[0], "roundrobin")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001338 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1339 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001340 }
Willy Tarreau9757a382009-10-03 12:56:50 +02001341 else if (!strcmp(args[0], "static-rr")) {
1342 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1343 curproxy->lbprm.algo |= BE_LB_ALGO_SRR;
1344 }
Willy Tarreauf09c6602012-02-13 17:12:08 +01001345 else if (!strcmp(args[0], "first")) {
1346 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1347 curproxy->lbprm.algo |= BE_LB_ALGO_FAS;
1348 }
Willy Tarreau51406232008-03-10 22:04:20 +01001349 else if (!strcmp(args[0], "leastconn")) {
1350 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1351 curproxy->lbprm.algo |= BE_LB_ALGO_LC;
1352 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001353 else if (!strcmp(args[0], "source")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001354 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1355 curproxy->lbprm.algo |= BE_LB_ALGO_SH;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001356 }
1357 else if (!strcmp(args[0], "uri")) {
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001358 int arg = 1;
1359
Willy Tarreau31682232007-11-29 15:38:04 +01001360 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1361 curproxy->lbprm.algo |= BE_LB_ALGO_UH;
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001362
Oskar Stolc8dc41842012-05-19 10:19:54 +01001363 curproxy->uri_whole = 0;
1364
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001365 while (*args[arg]) {
1366 if (!strcmp(args[arg], "len")) {
1367 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001368 memprintf(err, "%s : '%s' expects a positive integer (got '%s').", args[0], args[arg], args[arg+1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001369 return -1;
1370 }
1371 curproxy->uri_len_limit = atoi(args[arg+1]);
1372 arg += 2;
1373 }
1374 else if (!strcmp(args[arg], "depth")) {
1375 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001376 memprintf(err, "%s : '%s' expects a positive integer (got '%s').", args[0], args[arg], args[arg+1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001377 return -1;
1378 }
1379 /* hint: we store the position of the ending '/' (depth+1) so
1380 * that we avoid a comparison while computing the hash.
1381 */
1382 curproxy->uri_dirs_depth1 = atoi(args[arg+1]) + 1;
1383 arg += 2;
1384 }
Oskar Stolc8dc41842012-05-19 10:19:54 +01001385 else if (!strcmp(args[arg], "whole")) {
1386 curproxy->uri_whole = 1;
1387 arg += 1;
1388 }
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001389 else {
Oskar Stolc8dc41842012-05-19 10:19:54 +01001390 memprintf(err, "%s only accepts parameters 'len', 'depth', and 'whole' (got '%s').", args[0], args[arg]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001391 return -1;
1392 }
1393 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001394 }
Willy Tarreau01732802007-11-01 22:48:15 +01001395 else if (!strcmp(args[0], "url_param")) {
1396 if (!*args[1]) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001397 memprintf(err, "%s requires an URL parameter name.", args[0]);
Willy Tarreau01732802007-11-01 22:48:15 +01001398 return -1;
1399 }
Willy Tarreau31682232007-11-29 15:38:04 +01001400 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1401 curproxy->lbprm.algo |= BE_LB_ALGO_PH;
Willy Tarreaua534fea2008-08-03 12:19:50 +02001402
1403 free(curproxy->url_param_name);
Willy Tarreau01732802007-11-01 22:48:15 +01001404 curproxy->url_param_name = strdup(args[1]);
Willy Tarreaua534fea2008-08-03 12:19:50 +02001405 curproxy->url_param_len = strlen(args[1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001406 if (*args[2]) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001407 if (strcmp(args[2], "check_post")) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001408 memprintf(err, "%s only accepts 'check_post' modifier (got '%s').", args[0], args[2]);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001409 return -1;
1410 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001411 }
Benoitaffb4812009-03-25 13:02:10 +01001412 }
1413 else if (!strncmp(args[0], "hdr(", 4)) {
1414 const char *beg, *end;
1415
1416 beg = args[0] + 4;
1417 end = strchr(beg, ')');
1418
1419 if (!end || end == beg) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001420 memprintf(err, "hdr requires an http header field name.");
Benoitaffb4812009-03-25 13:02:10 +01001421 return -1;
1422 }
1423
1424 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1425 curproxy->lbprm.algo |= BE_LB_ALGO_HH;
1426
1427 free(curproxy->hh_name);
1428 curproxy->hh_len = end - beg;
1429 curproxy->hh_name = my_strndup(beg, end - beg);
1430 curproxy->hh_match_domain = 0;
1431
1432 if (*args[1]) {
1433 if (strcmp(args[1], "use_domain_only")) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001434 memprintf(err, "%s only accepts 'use_domain_only' modifier (got '%s').", args[0], args[1]);
Benoitaffb4812009-03-25 13:02:10 +01001435 return -1;
1436 }
1437 curproxy->hh_match_domain = 1;
1438 }
Emeric Brun736aa232009-06-30 17:56:00 +02001439 }
1440 else if (!strncmp(args[0], "rdp-cookie", 10)) {
1441 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1442 curproxy->lbprm.algo |= BE_LB_ALGO_RCH;
1443
1444 if ( *(args[0] + 10 ) == '(' ) { /* cookie name */
1445 const char *beg, *end;
1446
1447 beg = args[0] + 11;
1448 end = strchr(beg, ')');
1449
1450 if (!end || end == beg) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001451 memprintf(err, "rdp-cookie : missing cookie name.");
Emeric Brun736aa232009-06-30 17:56:00 +02001452 return -1;
1453 }
1454
1455 free(curproxy->hh_name);
1456 curproxy->hh_name = my_strndup(beg, end - beg);
1457 curproxy->hh_len = end - beg;
1458 }
1459 else if ( *(args[0] + 10 ) == '\0' ) { /* default cookie name 'mstshash' */
1460 free(curproxy->hh_name);
1461 curproxy->hh_name = strdup("mstshash");
1462 curproxy->hh_len = strlen(curproxy->hh_name);
1463 }
1464 else { /* syntax */
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001465 memprintf(err, "rdp-cookie : missing cookie name.");
Emeric Brun736aa232009-06-30 17:56:00 +02001466 return -1;
1467 }
Willy Tarreau01732802007-11-01 22:48:15 +01001468 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001469 else {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001470 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 +01001471 return -1;
1472 }
1473 return 0;
1474}
1475
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001476
1477/************************************************************************/
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001478/* All supported sample and ACL keywords must be declared here. */
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001479/************************************************************************/
1480
Willy Tarreau34db1082012-04-19 17:16:54 +02001481/* set temp integer to the number of enabled servers on the proxy.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001482 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001483 * undefined behaviour.
1484 */
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001485static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001486smp_fetch_nbsrv(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001487 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001488{
Willy Tarreau37406352012-04-23 16:16:37 +02001489 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001490 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001491 px = args->data.prx;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001492
1493 if (px->srv_act)
Willy Tarreauf853c462012-04-23 18:53:56 +02001494 smp->data.uint = px->srv_act;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001495 else if (px->lbprm.fbck)
Willy Tarreauf853c462012-04-23 18:53:56 +02001496 smp->data.uint = 1;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001497 else
Willy Tarreauf853c462012-04-23 18:53:56 +02001498 smp->data.uint = px->srv_bck;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001499
1500 return 1;
1501}
1502
Willy Tarreau37406352012-04-23 16:16:37 +02001503/* report in smp->flags a success or failure depending on the designated
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001504 * server's state. There is no match function involved since there's no pattern.
Willy Tarreau34db1082012-04-19 17:16:54 +02001505 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1506 * undefined behaviour.
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001507 */
1508static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001509smp_fetch_srv_is_up(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001510 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001511{
Willy Tarreau24e32d82012-04-23 23:55:44 +02001512 struct server *srv = args->data.srv;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001513
Willy Tarreau37406352012-04-23 16:16:37 +02001514 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001515 smp->type = SMP_T_BOOL;
Willy Tarreau20125212014-05-13 19:44:56 +02001516 if (!(srv->admin & SRV_ADMF_MAINT) &&
Willy Tarreau892337c2014-05-13 23:41:20 +02001517 (!(srv->check.state & CHK_ST_CONFIGURED) || (srv->state != SRV_ST_STOPPED)))
Willy Tarreau197e10a2012-04-23 19:18:42 +02001518 smp->data.uint = 1;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001519 else
Willy Tarreau197e10a2012-04-23 19:18:42 +02001520 smp->data.uint = 0;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001521 return 1;
1522}
1523
Willy Tarreau34db1082012-04-19 17:16:54 +02001524/* set temp integer to the number of enabled servers on the proxy.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001525 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001526 * undefined behaviour.
1527 */
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001528static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001529smp_fetch_connslots(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001530 const struct arg *args, struct sample *smp, const char *kw)
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001531{
1532 struct server *iterator;
Willy Tarreaud28c3532012-04-19 19:28:33 +02001533
Willy Tarreau37406352012-04-23 16:16:37 +02001534 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001535 smp->type = SMP_T_UINT;
1536 smp->data.uint = 0;
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001537
Willy Tarreau24e32d82012-04-23 23:55:44 +02001538 for (iterator = args->data.prx->srv; iterator; iterator = iterator->next) {
Willy Tarreau892337c2014-05-13 23:41:20 +02001539 if (iterator->state == SRV_ST_STOPPED)
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001540 continue;
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001541
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001542 if (iterator->maxconn == 0 || iterator->maxqueue == 0) {
Willy Tarreaua5e37562011-12-16 17:06:15 +01001543 /* configuration is stupid */
Willy Tarreauf853c462012-04-23 18:53:56 +02001544 smp->data.uint = -1; /* FIXME: stupid value! */
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001545 return 1;
1546 }
1547
Willy Tarreauf853c462012-04-23 18:53:56 +02001548 smp->data.uint += (iterator->maxconn - iterator->cur_sess)
Willy Tarreau422aa072012-04-20 20:49:27 +02001549 + (iterator->maxqueue - iterator->nbpend);
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001550 }
1551
1552 return 1;
1553}
1554
Willy Tarreaua5e37562011-12-16 17:06:15 +01001555/* set temp integer to the id of the backend */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001556static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001557smp_fetch_be_id(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001558 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau37406352012-04-23 16:16:37 +02001559{
Willy Tarreauf853c462012-04-23 18:53:56 +02001560 smp->flags = SMP_F_VOL_TXN;
1561 smp->type = SMP_T_UINT;
1562 smp->data.uint = l4->be->uuid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001563 return 1;
1564}
1565
Willy Tarreaua5e37562011-12-16 17:06:15 +01001566/* set temp integer to the id of the server */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001567static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001568smp_fetch_srv_id(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001569 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau37406352012-04-23 16:16:37 +02001570{
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001571 if (!objt_server(l4->target))
Willy Tarreau17af4192011-02-23 14:27:06 +01001572 return 0;
1573
Willy Tarreauf853c462012-04-23 18:53:56 +02001574 smp->type = SMP_T_UINT;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001575 smp->data.uint = objt_server(l4->target)->puid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001576
1577 return 1;
1578}
1579
Willy Tarreau34db1082012-04-19 17:16:54 +02001580/* set temp integer to the number of connections per second reaching the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001581 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001582 * undefined behaviour.
1583 */
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001584static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001585smp_fetch_be_sess_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001586 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001587{
Willy Tarreau37406352012-04-23 16:16:37 +02001588 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001589 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001590 smp->data.uint = read_freq_ctr(&args->data.prx->be_sess_per_sec);
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001591 return 1;
1592}
1593
Willy Tarreau34db1082012-04-19 17:16:54 +02001594/* set temp integer to the number of concurrent connections on the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001595 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001596 * undefined behaviour.
1597 */
Willy Tarreaua36af912009-10-10 12:02:45 +02001598static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001599smp_fetch_be_conn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001600 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua36af912009-10-10 12:02:45 +02001601{
Willy Tarreau37406352012-04-23 16:16:37 +02001602 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001603 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001604 smp->data.uint = args->data.prx->beconn;
Willy Tarreaua36af912009-10-10 12:02:45 +02001605 return 1;
1606}
1607
Willy Tarreau34db1082012-04-19 17:16:54 +02001608/* set temp integer to the total number of queued connections on the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001609 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001610 * undefined behaviour.
1611 */
Willy Tarreaua36af912009-10-10 12:02:45 +02001612static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001613smp_fetch_queue_size(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001614 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua36af912009-10-10 12:02:45 +02001615{
Willy Tarreau37406352012-04-23 16:16:37 +02001616 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001617 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001618 smp->data.uint = args->data.prx->totpend;
Willy Tarreaua36af912009-10-10 12:02:45 +02001619 return 1;
1620}
1621
Willy Tarreaua5e37562011-12-16 17:06:15 +01001622/* set temp integer to the total number of queued connections on the backend divided
Willy Tarreaua36af912009-10-10 12:02:45 +02001623 * by the number of running servers and rounded up. If there is no running
1624 * server, we return twice the total, just as if we had half a running server.
1625 * This is more or less correct anyway, since we expect the last server to come
1626 * back soon.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001627 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001628 * undefined behaviour.
Willy Tarreaua36af912009-10-10 12:02:45 +02001629 */
1630static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001631smp_fetch_avg_queue_size(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001632 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua36af912009-10-10 12:02:45 +02001633{
1634 int nbsrv;
1635
Willy Tarreau37406352012-04-23 16:16:37 +02001636 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001637 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001638 px = args->data.prx;
Willy Tarreaua36af912009-10-10 12:02:45 +02001639
1640 if (px->srv_act)
1641 nbsrv = px->srv_act;
1642 else if (px->lbprm.fbck)
1643 nbsrv = 1;
1644 else
1645 nbsrv = px->srv_bck;
1646
1647 if (nbsrv > 0)
Willy Tarreauf853c462012-04-23 18:53:56 +02001648 smp->data.uint = (px->totpend + nbsrv - 1) / nbsrv;
Willy Tarreaua36af912009-10-10 12:02:45 +02001649 else
Willy Tarreauf853c462012-04-23 18:53:56 +02001650 smp->data.uint = px->totpend * 2;
Willy Tarreaua36af912009-10-10 12:02:45 +02001651
1652 return 1;
1653}
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001654
Willy Tarreau34db1082012-04-19 17:16:54 +02001655/* set temp integer to the number of concurrent connections on the server in the backend.
1656 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1657 * undefined behaviour.
1658 */
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001659static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001660smp_fetch_srv_conn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001661 const struct arg *args, struct sample *smp, const char *kw)
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001662{
Willy Tarreauf853c462012-04-23 18:53:56 +02001663 smp->flags = SMP_F_VOL_TEST;
1664 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001665 smp->data.uint = args->data.srv->cur_sess;
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001666 return 1;
1667}
1668
Tait Clarridge7896d522012-12-05 21:39:31 -05001669/* set temp integer to the number of enabled servers on the proxy.
1670 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1671 * undefined behaviour.
1672 */
1673static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001674smp_fetch_srv_sess_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001675 const struct arg *args, struct sample *smp, const char *kw)
Tait Clarridge7896d522012-12-05 21:39:31 -05001676{
1677 smp->flags = SMP_F_VOL_TEST;
1678 smp->type = SMP_T_UINT;
1679 smp->data.uint = read_freq_ctr(&args->data.srv->sess_per_sec);
1680 return 1;
1681}
1682
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001683
1684/* Note: must not be declared <const> as its list will be overwritten.
1685 * Please take care of keeping this list alphabetically sorted.
1686 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001687static struct sample_fetch_kw_list smp_kws = {ILH, {
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001688 { "avg_queue", smp_fetch_avg_queue_size, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1689 { "be_conn", smp_fetch_be_conn, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1690 { "be_id", smp_fetch_be_id, 0, NULL, SMP_T_UINT, SMP_USE_BKEND, },
1691 { "be_sess_rate", smp_fetch_be_sess_rate, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1692 { "connslots", smp_fetch_connslots, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1693 { "nbsrv", smp_fetch_nbsrv, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1694 { "queue", smp_fetch_queue_size, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1695 { "srv_conn", smp_fetch_srv_conn, ARG1(1,SRV), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1696 { "srv_id", smp_fetch_srv_id, 0, NULL, SMP_T_UINT, SMP_USE_SERVR, },
1697 { "srv_is_up", smp_fetch_srv_is_up, ARG1(1,SRV), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
1698 { "srv_sess_rate", smp_fetch_srv_sess_rate, ARG1(1,SRV), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1699 { /* END */ },
1700}};
1701
1702
Willy Tarreau61612d42012-04-19 18:42:05 +02001703/* Note: must not be declared <const> as its list will be overwritten.
1704 * Please take care of keeping this list alphabetically sorted.
1705 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001706static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001707 { /* END */ },
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001708}};
1709
1710
1711__attribute__((constructor))
1712static void __backend_init(void)
1713{
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001714 sample_register_fetches(&smp_kws);
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001715 acl_register_keywords(&acl_kws);
1716}
1717
Willy Tarreaubaaee002006-06-26 02:48:02 +02001718/*
1719 * Local variables:
1720 * c-indent-level: 8
1721 * c-basic-offset: 8
1722 * End:
1723 */