blob: 01867684d577d589a5ffa79e85e14b67462c99b0 [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 }
natalie.chene22be3d2015-06-25 09:33:52 +0800215
216 //get rid of redundant '/'
217 while ((end - start) > 1 && *(start+1) == '/')
218 start++;
219 while ((end - start) > 1 && *(end-1) == '/')
220 end--;
221 //get rid of trailing ".git"
222 if ((end - start) > 4 && strcmp(end-4, ".git") == 0)
223 end -= 4;
Bhaskar98634f02013-10-29 23:30:51 -0400224
225 hash = gen_hash(px, start, (end - start));
226
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500227 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100228 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200229 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200230 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
231 return chash_get_server_hash(px, hash);
232 else
233 return map_get_server_hash(px, hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200234}
235
Willy Tarreau01732802007-11-01 22:48:15 +0100236/*
237 * This function tries to find a running server for the proxy <px> following
238 * the URL parameter hash method. It looks for a specific parameter in the
239 * URL and hashes it to compute the server ID. This is useful to optimize
240 * performance by avoiding bounces between servers in contexts where sessions
241 * are shared but cookies are not usable. If the parameter is not found, NULL
242 * is returned. If any server is found, it will be returned. If no valid server
243 * is found, NULL is returned.
Willy Tarreau01732802007-11-01 22:48:15 +0100244 */
245struct server *get_server_ph(struct proxy *px, const char *uri, int uri_len)
246{
Dan Dubovik82456752014-07-08 08:51:03 -0700247 unsigned int hash = 0;
Bhaskar98634f02013-10-29 23:30:51 -0400248 const char *start, *end;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200249 const char *p;
250 const char *params;
Willy Tarreau01732802007-11-01 22:48:15 +0100251 int plen;
252
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200253 /* when tot_weight is 0 then so is srv_count */
Willy Tarreau20697042007-11-15 23:26:18 +0100254 if (px->lbprm.tot_weight == 0)
Willy Tarreau01732802007-11-01 22:48:15 +0100255 return NULL;
256
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200257 if ((p = memchr(uri, '?', uri_len)) == NULL)
258 return NULL;
259
Willy Tarreau01732802007-11-01 22:48:15 +0100260 p++;
261
262 uri_len -= (p - uri);
263 plen = px->url_param_len;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200264 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100265
266 while (uri_len > plen) {
267 /* Look for the parameter name followed by an equal symbol */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200268 if (params[plen] == '=') {
269 if (memcmp(params, px->url_param_name, plen) == 0) {
270 /* OK, we have the parameter here at <params>, and
Willy Tarreau01732802007-11-01 22:48:15 +0100271 * the value after the equal sign, at <p>
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200272 * skip the equal symbol
Willy Tarreau01732802007-11-01 22:48:15 +0100273 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200274 p += plen + 1;
Bhaskar98634f02013-10-29 23:30:51 -0400275 start = end = p;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200276 uri_len -= plen + 1;
277
Bhaskar98634f02013-10-29 23:30:51 -0400278 while (uri_len && *end != '&') {
Willy Tarreau01732802007-11-01 22:48:15 +0100279 uri_len--;
Bhaskar98634f02013-10-29 23:30:51 -0400280 end++;
Willy Tarreau01732802007-11-01 22:48:15 +0100281 }
Bhaskar98634f02013-10-29 23:30:51 -0400282 hash = gen_hash(px, start, (end - start));
283
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500284 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100285 hash = full_hash(hash);
Bhaskar98634f02013-10-29 23:30:51 -0400286
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200287 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
288 return chash_get_server_hash(px, hash);
289 else
290 return map_get_server_hash(px, hash);
Willy Tarreau01732802007-11-01 22:48:15 +0100291 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200292 }
293 /* skip to next parameter */
294 p = memchr(params, '&', uri_len);
295 if (!p)
296 return NULL;
297 p++;
298 uri_len -= (p - params);
299 params = p;
300 }
301 return NULL;
302}
303
304/*
305 * this does the same as the previous server_ph, but check the body contents
306 */
307struct server *get_server_ph_post(struct session *s)
308{
Dan Dubovik82456752014-07-08 08:51:03 -0700309 unsigned int hash = 0;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200310 struct http_txn *txn = &s->txn;
Willy Tarreau7421efb2012-07-02 15:11:27 +0200311 struct channel *req = s->req;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200312 struct http_msg *msg = &txn->req;
313 struct proxy *px = s->be;
314 unsigned int plen = px->url_param_len;
Willy Tarreau2d8e4852014-04-17 20:08:17 +0200315 unsigned long len = http_body_bytes(msg);
Willy Tarreau0d090502014-04-17 20:31:44 +0200316 const char *params = b_ptr(req->buf, -http_data_rewind(msg));
Willy Tarreau157dd632009-12-06 19:18:09 +0100317 const char *p = params;
Bhaskar98634f02013-10-29 23:30:51 -0400318 const char *start, *end;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200319
Willy Tarreau157dd632009-12-06 19:18:09 +0100320 if (len == 0)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200321 return NULL;
322
Willy Tarreau522aab32015-05-02 00:05:47 +0200323 if (len > req->buf->data + req->buf->size - p)
324 len = req->buf->data + req->buf->size - p;
325
Willy Tarreau157dd632009-12-06 19:18:09 +0100326 if (px->lbprm.tot_weight == 0)
327 return NULL;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200328
329 while (len > plen) {
330 /* Look for the parameter name followed by an equal symbol */
331 if (params[plen] == '=') {
332 if (memcmp(params, px->url_param_name, plen) == 0) {
333 /* OK, we have the parameter here at <params>, and
334 * the value after the equal sign, at <p>
335 * skip the equal symbol
336 */
337 p += plen + 1;
Bhaskar98634f02013-10-29 23:30:51 -0400338 start = end = p;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200339 len -= plen + 1;
340
Bhaskar98634f02013-10-29 23:30:51 -0400341 while (len && *end != '&') {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200342 if (unlikely(!HTTP_IS_TOKEN(*p))) {
Willy Tarreau157dd632009-12-06 19:18:09 +0100343 /* if in a POST, body must be URI encoded or it's not a URI.
Bhaskar98634f02013-10-29 23:30:51 -0400344 * Do not interpret any possible binary data as a parameter.
Willy Tarreau157dd632009-12-06 19:18:09 +0100345 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200346 if (likely(HTTP_IS_LWS(*p))) /* eol, uncertain uri len */
347 break;
348 return NULL; /* oh, no; this is not uri-encoded.
349 * This body does not contain parameters.
350 */
351 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200352 len--;
Bhaskar98634f02013-10-29 23:30:51 -0400353 end++;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200354 /* should we break if vlen exceeds limit? */
355 }
Bhaskar98634f02013-10-29 23:30:51 -0400356 hash = gen_hash(px, start, (end - start));
357
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500358 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100359 hash = full_hash(hash);
Bhaskar98634f02013-10-29 23:30:51 -0400360
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200361 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
362 return chash_get_server_hash(px, hash);
363 else
364 return map_get_server_hash(px, hash);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200365 }
366 }
Willy Tarreau01732802007-11-01 22:48:15 +0100367 /* skip to next parameter */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200368 p = memchr(params, '&', len);
Willy Tarreau01732802007-11-01 22:48:15 +0100369 if (!p)
370 return NULL;
371 p++;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200372 len -= (p - params);
373 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100374 }
375 return NULL;
376}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200377
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200378
Willy Tarreaubaaee002006-06-26 02:48:02 +0200379/*
Benoitaffb4812009-03-25 13:02:10 +0100380 * This function tries to find a running server for the proxy <px> following
381 * the Header parameter hash method. It looks for a specific parameter in the
382 * URL and hashes it to compute the server ID. This is useful to optimize
383 * performance by avoiding bounces between servers in contexts where sessions
384 * are shared but cookies are not usable. If the parameter is not found, NULL
385 * is returned. If any server is found, it will be returned. If no valid server
386 * is found, NULL is returned.
387 */
388struct server *get_server_hh(struct session *s)
389{
Dan Dubovik82456752014-07-08 08:51:03 -0700390 unsigned int hash = 0;
Benoitaffb4812009-03-25 13:02:10 +0100391 struct http_txn *txn = &s->txn;
Benoitaffb4812009-03-25 13:02:10 +0100392 struct proxy *px = s->be;
393 unsigned int plen = px->hh_len;
394 unsigned long len;
395 struct hdr_ctx ctx;
396 const char *p;
Bhaskar98634f02013-10-29 23:30:51 -0400397 const char *start, *end;
Benoitaffb4812009-03-25 13:02:10 +0100398
399 /* tot_weight appears to mean srv_count */
400 if (px->lbprm.tot_weight == 0)
401 return NULL;
402
Benoitaffb4812009-03-25 13:02:10 +0100403 ctx.idx = 0;
404
405 /* if the message is chunked, we skip the chunk size, but use the value as len */
Willy Tarreau211cdec2014-04-17 20:18:08 +0200406 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 +0100407
408 /* if the header is not found or empty, let's fallback to round robin */
409 if (!ctx.idx || !ctx.vlen)
410 return NULL;
411
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200412 /* note: we won't hash if there's only one server left */
413 if (px->lbprm.tot_used == 1)
414 goto hash_done;
415
Benoitaffb4812009-03-25 13:02:10 +0100416 /* Found a the hh_name in the headers.
417 * we will compute the hash based on this value ctx.val.
418 */
419 len = ctx.vlen;
420 p = (char *)ctx.line + ctx.val;
421 if (!px->hh_match_domain) {
Bhaskar98634f02013-10-29 23:30:51 -0400422 hash = gen_hash(px, p, len);
Benoitaffb4812009-03-25 13:02:10 +0100423 } else {
424 int dohash = 0;
Cyril Bonté7ccea262015-01-04 15:17:36 +0100425 p += len;
Benoitaffb4812009-03-25 13:02:10 +0100426 /* special computation, use only main domain name, not tld/host
427 * going back from the end of string, start hashing at first
428 * dot stop at next.
429 * This is designed to work with the 'Host' header, and requires
430 * a special option to activate this.
431 */
Cyril Bonté7ccea262015-01-04 15:17:36 +0100432 end = p;
Benoitaffb4812009-03-25 13:02:10 +0100433 while (len) {
Cyril Bonté7ccea262015-01-04 15:17:36 +0100434 if (dohash) {
435 /* Rewind the pointer until the previous char
436 * is a dot, this will allow to set the start
437 * position of the domain. */
438 if (*(p - 1) == '.')
Benoitaffb4812009-03-25 13:02:10 +0100439 break;
Benoitaffb4812009-03-25 13:02:10 +0100440 }
Cyril Bonté7ccea262015-01-04 15:17:36 +0100441 else if (*p == '.') {
442 /* The pointer is rewinded to the dot before the
443 * tld, we memorize the end of the domain and
444 * can enter the domain processing. */
445 end = p;
446 dohash = 1;
447 }
Benoitaffb4812009-03-25 13:02:10 +0100448 p--;
Cyril Bonté7ccea262015-01-04 15:17:36 +0100449 len--;
Benoitaffb4812009-03-25 13:02:10 +0100450 }
Cyril Bonté7ccea262015-01-04 15:17:36 +0100451 start = p;
Bhaskar98634f02013-10-29 23:30:51 -0400452 hash = gen_hash(px, start, (end - start));
Benoitaffb4812009-03-25 13:02:10 +0100453 }
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500454 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100455 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200456 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200457 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
458 return chash_get_server_hash(px, hash);
459 else
460 return map_get_server_hash(px, hash);
Benoitaffb4812009-03-25 13:02:10 +0100461}
462
Willy Tarreau34db1082012-04-19 17:16:54 +0200463/* RDP Cookie HASH. */
Emeric Brun736aa232009-06-30 17:56:00 +0200464struct server *get_server_rch(struct session *s)
465{
Dan Dubovik82456752014-07-08 08:51:03 -0700466 unsigned int hash = 0;
Emeric Brun736aa232009-06-30 17:56:00 +0200467 struct proxy *px = s->be;
468 unsigned long len;
Emeric Brun736aa232009-06-30 17:56:00 +0200469 int ret;
Willy Tarreau37406352012-04-23 16:16:37 +0200470 struct sample smp;
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200471 int rewind;
Emeric Brun736aa232009-06-30 17:56:00 +0200472
473 /* tot_weight appears to mean srv_count */
474 if (px->lbprm.tot_weight == 0)
475 return NULL;
476
Willy Tarreau37406352012-04-23 16:16:37 +0200477 memset(&smp, 0, sizeof(smp));
Emeric Brun736aa232009-06-30 17:56:00 +0200478
Willy Tarreau9b28e032012-10-12 23:49:43 +0200479 b_rew(s->req->buf, rewind = s->req->buf->o);
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200480
Willy Tarreaucadd8c92013-07-22 18:09:52 +0200481 ret = fetch_rdp_cookie_name(s, &smp, px->hh_name, px->hh_len);
Willy Tarreauf853c462012-04-23 18:53:56 +0200482 len = smp.data.str.len;
Willy Tarreau664092c2011-12-16 19:11:42 +0100483
Willy Tarreau9b28e032012-10-12 23:49:43 +0200484 b_adv(s->req->buf, rewind);
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200485
Willy Tarreau37406352012-04-23 16:16:37 +0200486 if (ret == 0 || (smp.flags & SMP_F_MAY_CHANGE) || len == 0)
Emeric Brun736aa232009-06-30 17:56:00 +0200487 return NULL;
488
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200489 /* note: we won't hash if there's only one server left */
490 if (px->lbprm.tot_used == 1)
491 goto hash_done;
492
Emeric Brun736aa232009-06-30 17:56:00 +0200493 /* Found a the hh_name in the headers.
494 * we will compute the hash based on this value ctx.val.
495 */
Bhaskar98634f02013-10-29 23:30:51 -0400496 hash = gen_hash(px, smp.data.str.str, len);
497
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500498 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100499 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200500 hash_done:
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200501 if (px->lbprm.algo & BE_LB_LKUP_CHTREE)
502 return chash_get_server_hash(px, hash);
503 else
504 return map_get_server_hash(px, hash);
Emeric Brun736aa232009-06-30 17:56:00 +0200505}
Benoitaffb4812009-03-25 13:02:10 +0100506
507/*
Willy Tarreau7c669d72008-06-20 15:04:11 +0200508 * This function applies the load-balancing algorithm to the session, as
509 * defined by the backend it is assigned to. The session is then marked as
510 * 'assigned'.
511 *
512 * This function MAY NOT be called with SN_ASSIGNED already set. If the session
513 * had a server previously assigned, it is rebalanced, trying to avoid the same
Willy Tarreau827aee92011-03-10 16:55:02 +0100514 * server, which should still be present in target_srv(&s->target) before the call.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200515 * The function tries to keep the original connection slot if it reconnects to
516 * the same server, otherwise it releases it and tries to offer it.
517 *
518 * It is illegal to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200519 *
520 * It may return :
Willy Tarreau664beb82011-03-10 11:38:29 +0100521 * SRV_STATUS_OK if everything is OK. ->srv and ->target are assigned.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200522 * SRV_STATUS_NOSRV if no server is available. Session is not ASSIGNED
523 * SRV_STATUS_FULL if all servers are saturated. Session is not ASSIGNED
Willy Tarreaubaaee002006-06-26 02:48:02 +0200524 * SRV_STATUS_INTERNAL for other unrecoverable errors.
525 *
Willy Tarreau7c669d72008-06-20 15:04:11 +0200526 * Upon successful return, the session flag SN_ASSIGNED is set to indicate that
Willy Tarreau827aee92011-03-10 16:55:02 +0100527 * it does not need to be called anymore. This means that target_srv(&s->target)
528 * can be trusted in balance and direct modes.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200529 *
530 */
531
532int assign_server(struct session *s)
533{
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200534 struct connection *conn;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200535 struct server *conn_slot;
Willy Tarreau827aee92011-03-10 16:55:02 +0100536 struct server *srv, *prev_srv;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200537 int err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100538
Simon Horman8effd3d2011-08-13 08:03:52 +0900539 DPRINTF(stderr,"assign_server : s=%p\n",s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200540
Willy Tarreau7c669d72008-06-20 15:04:11 +0200541 err = SRV_STATUS_INTERNAL;
542 if (unlikely(s->pend_pos || s->flags & SN_ASSIGNED))
543 goto out_err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100544
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100545 prev_srv = objt_server(s->target);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200546 conn_slot = s->srv_conn;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200547
Willy Tarreau7c669d72008-06-20 15:04:11 +0200548 /* We have to release any connection slot before applying any LB algo,
549 * otherwise we may erroneously end up with no available slot.
550 */
551 if (conn_slot)
552 sess_change_server(s, NULL);
553
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100554 /* We will now try to find the good server and store it into <objt_server(s->target)>.
555 * Note that <objt_server(s->target)> may be NULL in case of dispatch or proxy mode,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200556 * as well as if no server is available (check error code).
557 */
Willy Tarreau1a20a5d2007-11-01 21:08:19 +0100558
Willy Tarreau827aee92011-03-10 16:55:02 +0100559 srv = NULL;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100560 s->target = NULL;
Willy Tarreau9420b122013-12-15 18:58:25 +0100561 conn = objt_conn(s->req->cons->end);
Willy Tarreau664beb82011-03-10 11:38:29 +0100562
Willy Tarreau068621e2013-12-23 15:11:25 +0100563 if (conn &&
Willy Tarreau2481d162014-02-19 18:40:43 +0100564 (conn->flags & CO_FL_CONNECTED) &&
Willy Tarreau9420b122013-12-15 18:58:25 +0100565 objt_server(conn->target) && __objt_server(conn->target)->proxy == s->be &&
Willy Tarreauc35362a2014-04-25 13:58:37 +0200566 ((s->txn.flags & TX_PREFER_LAST) ||
567 ((s->be->options & PR_O_PREF_LAST) &&
568 (!s->be->max_ka_queue ||
569 server_has_room(__objt_server(conn->target)) ||
570 (__objt_server(conn->target)->nbpend + 1) < s->be->max_ka_queue))) &&
Willy Tarreau87eb1d62014-05-13 18:51:40 +0200571 srv_is_usable(__objt_server(conn->target))) {
Willy Tarreau9420b122013-12-15 18:58:25 +0100572 /* This session was relying on a server in a previous request
Godbachcfbb93f2014-12-10 10:21:30 +0800573 * and the proxy has "option prefer-last-server" set, so
Willy Tarreau9420b122013-12-15 18:58:25 +0100574 * let's try to reuse the same server.
575 */
576 srv = __objt_server(conn->target);
577 s->target = &srv->obj_type;
578 }
579 else if (s->be->lbprm.algo & BE_LB_KIND) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200580 /* we must check if we have at least one server available */
581 if (!s->be->lbprm.tot_weight) {
582 err = SRV_STATUS_NOSRV;
583 goto out;
584 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200585
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200586 /* First check whether we need to fetch some data or simply call
587 * the LB lookup function. Only the hashing functions will need
588 * some input data in fact, and will support multiple algorithms.
589 */
590 switch (s->be->lbprm.algo & BE_LB_LKUP) {
591 case BE_LB_LKUP_RRTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100592 srv = fwrr_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200593 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200594
Willy Tarreauf09c6602012-02-13 17:12:08 +0100595 case BE_LB_LKUP_FSTREE:
596 srv = fas_get_next_server(s->be, prev_srv);
597 break;
598
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200599 case BE_LB_LKUP_LCTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100600 srv = fwlc_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200601 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200602
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200603 case BE_LB_LKUP_CHTREE:
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200604 case BE_LB_LKUP_MAP:
Willy Tarreau9757a382009-10-03 12:56:50 +0200605 if ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200606 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100607 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200608 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100609 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau9757a382009-10-03 12:56:50 +0200610 break;
611 }
612 else if ((s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_HI) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200613 /* unknown balancing algorithm */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200614 err = SRV_STATUS_INTERNAL;
615 goto out;
616 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200617
618 switch (s->be->lbprm.algo & BE_LB_PARM) {
619 case BE_LB_HASH_SRC:
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200620 conn = objt_conn(s->req->prod->end);
621 if (conn && conn->addr.from.ss_family == AF_INET) {
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200622 srv = get_server_sh(s->be,
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200623 (void *)&((struct sockaddr_in *)&conn->addr.from)->sin_addr,
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200624 4);
625 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200626 else if (conn && conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200627 srv = get_server_sh(s->be,
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200628 (void *)&((struct sockaddr_in6 *)&conn->addr.from)->sin6_addr,
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200629 16);
630 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200631 else {
632 /* unknown IP family */
633 err = SRV_STATUS_INTERNAL;
634 goto out;
635 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200636 break;
637
638 case BE_LB_HASH_URI:
639 /* URI hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100640 if (s->txn.req.msg_state < HTTP_MSG_BODY)
641 break;
Willy Tarreau827aee92011-03-10 16:55:02 +0100642 srv = get_server_uh(s->be,
Willy Tarreauda6eed62014-04-17 20:24:24 +0200643 b_ptr(s->req->buf, -http_uri_rewind(&s->txn.req)),
Willy Tarreau827aee92011-03-10 16:55:02 +0100644 s->txn.req.sl.rq.u_l);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200645 break;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200646
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200647 case BE_LB_HASH_PRM:
648 /* URL Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100649 if (s->txn.req.msg_state < HTTP_MSG_BODY)
650 break;
Willy Tarreau61a21a32011-03-01 20:35:49 +0100651
Willy Tarreau827aee92011-03-10 16:55:02 +0100652 srv = get_server_ph(s->be,
Willy Tarreauda6eed62014-04-17 20:24:24 +0200653 b_ptr(s->req->buf, -http_uri_rewind(&s->txn.req)),
Willy Tarreau827aee92011-03-10 16:55:02 +0100654 s->txn.req.sl.rq.u_l);
Willy Tarreau61a21a32011-03-01 20:35:49 +0100655
Willy Tarreau827aee92011-03-10 16:55:02 +0100656 if (!srv && s->txn.meth == HTTP_METH_POST)
657 srv = get_server_ph_post(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200658 break;
Benoitaffb4812009-03-25 13:02:10 +0100659
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200660 case BE_LB_HASH_HDR:
661 /* Header Parameter hashing */
Willy Tarreaueaed5a12010-03-24 14:54:30 +0100662 if (s->txn.req.msg_state < HTTP_MSG_BODY)
663 break;
Willy Tarreau827aee92011-03-10 16:55:02 +0100664 srv = get_server_hh(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200665 break;
666
667 case BE_LB_HASH_RDP:
668 /* RDP Cookie hashing */
Willy Tarreau827aee92011-03-10 16:55:02 +0100669 srv = get_server_rch(s);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200670 break;
671
672 default:
673 /* unknown balancing algorithm */
674 err = SRV_STATUS_INTERNAL;
675 goto out;
Benoitaffb4812009-03-25 13:02:10 +0100676 }
Emeric Brun736aa232009-06-30 17:56:00 +0200677
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200678 /* If the hashing parameter was not found, let's fall
679 * back to round robin on the map.
680 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100681 if (!srv) {
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200682 if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100683 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200684 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100685 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200686 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200687
688 /* end of map-based LB */
Emeric Brun736aa232009-06-30 17:56:00 +0200689 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200690
Willy Tarreau7c669d72008-06-20 15:04:11 +0200691 default:
692 /* unknown balancing algorithm */
693 err = SRV_STATUS_INTERNAL;
694 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200695 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200696
Willy Tarreau827aee92011-03-10 16:55:02 +0100697 if (!srv) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200698 err = SRV_STATUS_FULL;
699 goto out;
700 }
Willy Tarreau827aee92011-03-10 16:55:02 +0100701 else if (srv != prev_srv) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100702 s->be->be_counters.cum_lbconn++;
Willy Tarreau827aee92011-03-10 16:55:02 +0100703 srv->counters.cum_lbconn++;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100704 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100705 s->target = &srv->obj_type;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200706 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200707 else if (s->be->options & (PR_O_DISPATCH | PR_O_TRANSP)) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100708 s->target = &s->be->obj_type;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200709 }
David du Colombier6f5ccb12011-03-10 22:26:24 +0100710 else if ((s->be->options & PR_O_HTTP_PROXY) &&
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200711 (conn = objt_conn(s->req->cons->end)) &&
712 is_addr(&conn->addr.to)) {
Willy Tarreau664beb82011-03-10 11:38:29 +0100713 /* in proxy mode, we need a valid destination address */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100714 s->target = &s->be->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +0100715 }
716 else {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200717 err = SRV_STATUS_NOSRV;
718 goto out;
719 }
720
721 s->flags |= SN_ASSIGNED;
722 err = SRV_STATUS_OK;
723 out:
724
725 /* Either we take back our connection slot, or we offer it to someone
726 * else if we don't need it anymore.
727 */
728 if (conn_slot) {
Willy Tarreau827aee92011-03-10 16:55:02 +0100729 if (conn_slot == srv) {
730 sess_change_server(s, srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200731 } else {
732 if (may_dequeue_tasks(conn_slot, s->be))
733 process_srv_queue(conn_slot);
734 }
735 }
736
737 out_err:
738 return err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200739}
740
741
742/*
743 * This function assigns a server address to a session, and sets SN_ADDR_SET.
744 * The address is taken from the currently assigned server, or from the
745 * dispatch or transparent address.
746 *
747 * It may return :
748 * SRV_STATUS_OK if everything is OK.
749 * SRV_STATUS_INTERNAL for other unrecoverable errors.
750 *
751 * Upon successful return, the session flag SN_ADDR_SET is set. This flag is
752 * not cleared, so it's to the caller to clear it if required.
753 *
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200754 * The caller is responsible for having already assigned a connection
755 * to si->end.
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200756 *
Willy Tarreaubaaee002006-06-26 02:48:02 +0200757 */
758int assign_server_address(struct session *s)
759{
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200760 struct connection *cli_conn = objt_conn(s->req->prod->end);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200761 struct connection *srv_conn = objt_conn(s->req->cons->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200762
Willy Tarreaubaaee002006-06-26 02:48:02 +0200763#ifdef DEBUG_FULL
764 fprintf(stderr,"assign_server_address : s=%p\n",s);
765#endif
766
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200767 if ((s->flags & SN_DIRECT) || (s->be->lbprm.algo & BE_LB_KIND)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200768 /* A server is necessarily known for this session */
769 if (!(s->flags & SN_ASSIGNED))
770 return SRV_STATUS_INTERNAL;
771
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200772 srv_conn->addr.to = objt_server(s->target)->addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200773
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200774 if (!is_addr(&srv_conn->addr.to) && cli_conn) {
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200775 /* if the server has no address, we use the same address
776 * the client asked, which is handy for remapping ports
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200777 * locally on multiple addresses at once. Nothing is done
778 * for AF_UNIX addresses.
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200779 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200780 conn_get_to_addr(cli_conn);
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200781
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200782 if (cli_conn->addr.to.ss_family == AF_INET) {
783 ((struct sockaddr_in *)&srv_conn->addr.to)->sin_addr = ((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
784 } else if (cli_conn->addr.to.ss_family == AF_INET6) {
785 ((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 +0200786 }
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200787 }
788
Willy Tarreaubaaee002006-06-26 02:48:02 +0200789 /* if this server remaps proxied ports, we'll use
790 * the port the client connected to with an offset. */
Willy Tarreauc93cd162014-05-13 15:54:22 +0200791 if ((objt_server(s->target)->flags & SRV_F_MAPPORTS) && cli_conn) {
David du Colombier6f5ccb12011-03-10 22:26:24 +0100792 int base_port;
793
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200794 conn_get_to_addr(cli_conn);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100795
796 /* First, retrieve the port from the incoming connection */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200797 base_port = get_host_port(&cli_conn->addr.to);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100798
799 /* Second, assign the outgoing connection's port */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200800 base_port += get_host_port(&srv_conn->addr.to);
801 set_host_port(&srv_conn->addr.to, base_port);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200802 }
803 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200804 else if (s->be->options & PR_O_DISPATCH) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200805 /* connect to the defined dispatch addr */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200806 srv_conn->addr.to = s->be->dispatch_addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200807 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200808 else if ((s->be->options & PR_O_TRANSP) && cli_conn) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200809 /* in transparent mode, use the original dest addr if no dispatch specified */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200810 conn_get_to_addr(cli_conn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200811
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200812 if (cli_conn->addr.to.ss_family == AF_INET || cli_conn->addr.to.ss_family == AF_INET6)
813 srv_conn->addr.to = cli_conn->addr.to;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200814 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100815 else if (s->be->options & PR_O_HTTP_PROXY) {
816 /* If HTTP PROXY option is set, then server is already assigned
817 * during incoming client request parsing. */
818 }
Willy Tarreau1a1158b2007-01-20 11:07:46 +0100819 else {
820 /* no server and no LB algorithm ! */
821 return SRV_STATUS_INTERNAL;
822 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200823
824 s->flags |= SN_ADDR_SET;
825 return SRV_STATUS_OK;
826}
827
828
829/* This function assigns a server to session <s> if required, and can add the
830 * connection to either the assigned server's queue or to the proxy's queue.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200831 * If ->srv_conn is set, the session is first released from the server.
832 * It may also be called with SN_DIRECT and/or SN_ASSIGNED though. It will
833 * be called before any connection and after any retry or redispatch occurs.
834 *
835 * It is not allowed to call this function with a session in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200836 *
837 * Returns :
838 *
839 * SRV_STATUS_OK if everything is OK.
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100840 * SRV_STATUS_NOSRV if no server is available. objt_server(s->target) = NULL.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200841 * SRV_STATUS_QUEUED if the connection has been queued.
842 * SRV_STATUS_FULL if the server(s) is/are saturated and the
Willy Tarreau827aee92011-03-10 16:55:02 +0100843 * connection could not be queued at the server's,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200844 * which may be NULL if we queue on the backend.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200845 * SRV_STATUS_INTERNAL for other unrecoverable errors.
846 *
847 */
848int assign_server_and_queue(struct session *s)
849{
850 struct pendconn *p;
Willy Tarreau827aee92011-03-10 16:55:02 +0100851 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200852 int err;
853
854 if (s->pend_pos)
855 return SRV_STATUS_INTERNAL;
856
Willy Tarreau7c669d72008-06-20 15:04:11 +0200857 err = SRV_STATUS_OK;
858 if (!(s->flags & SN_ASSIGNED)) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100859 struct server *prev_srv = objt_server(s->target);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100860
Willy Tarreau7c669d72008-06-20 15:04:11 +0200861 err = assign_server(s);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100862 if (prev_srv) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200863 /* This session was previously assigned to a server. We have to
864 * update the session's and the server's stats :
865 * - if the server changed :
866 * - set TX_CK_DOWN if txn.flags was TX_CK_VALID
867 * - set SN_REDISP if it was successfully redispatched
868 * - increment srv->redispatches and be->redispatches
869 * - if the server remained the same : update retries.
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100870 */
871
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100872 if (prev_srv != objt_server(s->target)) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200873 if ((s->txn.flags & TX_CK_MASK) == TX_CK_VALID) {
874 s->txn.flags &= ~TX_CK_MASK;
875 s->txn.flags |= TX_CK_DOWN;
876 }
877 s->flags |= SN_REDISP;
Willy Tarreau3d80d912011-03-10 11:42:13 +0100878 prev_srv->counters.redispatches++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100879 s->be->be_counters.redispatches++;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200880 } else {
Willy Tarreau3d80d912011-03-10 11:42:13 +0100881 prev_srv->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100882 s->be->be_counters.retries++;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100883 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100884 }
885 }
886
Willy Tarreaubaaee002006-06-26 02:48:02 +0200887 switch (err) {
888 case SRV_STATUS_OK:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200889 /* we have SN_ASSIGNED set */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100890 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100891 if (!srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200892 return SRV_STATUS_OK; /* dispatch or proxy mode */
893
894 /* If we already have a connection slot, no need to check any queue */
Willy Tarreau827aee92011-03-10 16:55:02 +0100895 if (s->srv_conn == srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200896 return SRV_STATUS_OK;
897
898 /* OK, this session already has an assigned server, but no
899 * connection slot yet. Either it is a redispatch, or it was
900 * assigned from persistence information (direct mode).
901 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100902 if ((s->flags & SN_REDIRECTABLE) && srv->rdr_len) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200903 /* server scheduled for redirection, and already assigned. We
904 * don't want to go further nor check the queue.
Willy Tarreau21d2af32008-02-14 20:25:24 +0100905 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100906 sess_change_server(s, srv); /* not really needed in fact */
Willy Tarreau21d2af32008-02-14 20:25:24 +0100907 return SRV_STATUS_OK;
908 }
909
Willy Tarreau7c669d72008-06-20 15:04:11 +0200910 /* We might have to queue this session if the assigned server is full.
911 * We know we have to queue it into the server's queue, so if a maxqueue
912 * is set on the server, we must also check that the server's queue is
913 * not full, in which case we have to return FULL.
914 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100915 if (srv->maxconn &&
916 (srv->nbpend || srv->served >= srv_dynamic_maxconn(srv))) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200917
Willy Tarreau827aee92011-03-10 16:55:02 +0100918 if (srv->maxqueue > 0 && srv->nbpend >= srv->maxqueue)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200919 return SRV_STATUS_FULL;
920
Willy Tarreaubaaee002006-06-26 02:48:02 +0200921 p = pendconn_add(s);
922 if (p)
923 return SRV_STATUS_QUEUED;
924 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200925 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200926 }
Willy Tarreau7c669d72008-06-20 15:04:11 +0200927
928 /* OK, we can use this server. Let's reserve our place */
Willy Tarreau827aee92011-03-10 16:55:02 +0100929 sess_change_server(s, srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200930 return SRV_STATUS_OK;
931
932 case SRV_STATUS_FULL:
933 /* queue this session into the proxy's queue */
934 p = pendconn_add(s);
935 if (p)
936 return SRV_STATUS_QUEUED;
937 else
Willy Tarreau7c669d72008-06-20 15:04:11 +0200938 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200939
940 case SRV_STATUS_NOSRV:
Willy Tarreau7c669d72008-06-20 15:04:11 +0200941 return err;
942
Willy Tarreaubaaee002006-06-26 02:48:02 +0200943 case SRV_STATUS_INTERNAL:
944 return err;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200945
Willy Tarreaubaaee002006-06-26 02:48:02 +0200946 default:
947 return SRV_STATUS_INTERNAL;
948 }
Willy Tarreau5b6995c2008-01-13 16:31:17 +0100949}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200950
Willy Tarreaub1d67742010-03-29 19:36:59 +0200951/* If an explicit source binding is specified on the server and/or backend, and
952 * this source makes use of the transparent proxy, then it is extracted now and
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200953 * assigned to the session's pending connection. This function assumes that an
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200954 * outgoing connection has already been assigned to s->req->cons->end.
Willy Tarreaub1d67742010-03-29 19:36:59 +0200955 */
956static void assign_tproxy_address(struct session *s)
957{
Pieter Baauwd551fb52013-05-08 22:49:23 +0200958#if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_TRANSPARENT)
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100959 struct server *srv = objt_server(s->target);
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100960 struct conn_src *src;
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200961 struct connection *cli_conn;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200962 struct connection *srv_conn = objt_conn(s->req->cons->end);
Willy Tarreau827aee92011-03-10 16:55:02 +0100963
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100964 if (srv && srv->conn_src.opts & CO_SRC_BIND)
965 src = &srv->conn_src;
966 else if (s->be->conn_src.opts & CO_SRC_BIND)
967 src = &s->be->conn_src;
968 else
969 return;
Willy Tarreau294c4732011-12-16 21:35:50 +0100970
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100971 switch (src->opts & CO_SRC_TPROXY_MASK) {
972 case CO_SRC_TPROXY_ADDR:
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200973 srv_conn->addr.from = src->tproxy_addr;
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100974 break;
975 case CO_SRC_TPROXY_CLI:
976 case CO_SRC_TPROXY_CIP:
977 /* FIXME: what can we do if the client connects in IPv6 or unix socket ? */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200978 cli_conn = objt_conn(s->req->prod->end);
979 if (cli_conn)
980 srv_conn->addr.from = cli_conn->addr.from;
981 else
982 memset(&srv_conn->addr.from, 0, sizeof(srv_conn->addr.from));
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100983 break;
984 case CO_SRC_TPROXY_DYN:
985 if (src->bind_hdr_occ) {
986 char *vptr;
987 int vlen;
988 int rewind;
Willy Tarreau294c4732011-12-16 21:35:50 +0100989
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100990 /* bind to the IP in a header */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200991 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_family = AF_INET;
992 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_port = 0;
993 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_addr.s_addr = 0;
Willy Tarreau294c4732011-12-16 21:35:50 +0100994
Willy Tarreau211cdec2014-04-17 20:18:08 +0200995 b_rew(s->req->buf, rewind = http_hdr_rewind(&s->txn.req));
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100996 if (http_get_hdr(&s->txn.req, src->bind_hdr_name, src->bind_hdr_len,
997 &s->txn.hdr_idx, src->bind_hdr_occ, NULL, &vptr, &vlen)) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200998 ((struct sockaddr_in *)&srv_conn->addr.from)->sin_addr.s_addr =
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +0100999 htonl(inetaddr_host_lim(vptr, vptr + vlen));
Willy Tarreaubce70882009-09-07 11:51:47 +02001000 }
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +01001001 b_adv(s->req->buf, rewind);
Willy Tarreaub1d67742010-03-29 19:36:59 +02001002 }
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +01001003 break;
1004 default:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001005 memset(&srv_conn->addr.from, 0, sizeof(srv_conn->addr.from));
Willy Tarreaub1d67742010-03-29 19:36:59 +02001006 }
1007#endif
1008}
1009
1010
Willy Tarreaubaaee002006-06-26 02:48:02 +02001011/*
1012 * This function initiates a connection to the server assigned to this session
Willy Tarreau6471afb2011-09-23 10:54:59 +02001013 * (s->target, s->req->cons->addr.to). It will assign a server if none
Willy Tarreau664beb82011-03-10 11:38:29 +01001014 * is assigned yet.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001015 * It can return one of :
1016 * - SN_ERR_NONE if everything's OK
1017 * - SN_ERR_SRVTO if there are no more servers
1018 * - SN_ERR_SRVCL if the connection was refused by the server
1019 * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1020 * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1021 * - SN_ERR_INTERNAL for any other purely internal errors
1022 * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001023 * The server-facing stream interface is expected to hold a pre-allocated connection
1024 * in s->req->cons->conn.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001025 */
1026int connect_server(struct session *s)
1027{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001028 struct connection *cli_conn;
Willy Tarreau34601a82013-12-15 16:33:46 +01001029 struct connection *srv_conn;
Willy Tarreau827aee92011-03-10 16:55:02 +01001030 struct server *srv;
Willy Tarreau34601a82013-12-15 16:33:46 +01001031 int reuse = 0;
Willy Tarreau9650f372009-08-16 14:02:45 +02001032 int err;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001033
Willy Tarreau34601a82013-12-15 16:33:46 +01001034 srv_conn = objt_conn(s->req->cons->end);
1035 if (srv_conn)
1036 reuse = s->target == srv_conn->target;
1037
1038 if (reuse) {
1039 /* Disable connection reuse if a dynamic source is used.
1040 * As long as we don't share connections between servers,
1041 * we don't need to disable connection reuse on no-idempotent
1042 * requests nor when PROXY protocol is used.
1043 */
1044 srv = objt_server(s->target);
1045 if (srv && srv->conn_src.opts & CO_SRC_BIND) {
1046 if ((srv->conn_src.opts & CO_SRC_TPROXY_MASK) == CO_SRC_TPROXY_DYN)
1047 reuse = 0;
1048 }
1049 else if (s->be->conn_src.opts & CO_SRC_BIND) {
1050 if ((s->be->conn_src.opts & CO_SRC_TPROXY_MASK) == CO_SRC_TPROXY_DYN)
1051 reuse = 0;
1052 }
1053 }
1054
1055 srv_conn = si_alloc_conn(s->req->cons, reuse);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02001056 if (!srv_conn)
1057 return SN_ERR_RESOURCE;
1058
Willy Tarreaubaaee002006-06-26 02:48:02 +02001059 if (!(s->flags & SN_ADDR_SET)) {
1060 err = assign_server_address(s);
1061 if (err != SRV_STATUS_OK)
1062 return SN_ERR_INTERNAL;
1063 }
1064
Willy Tarreauff605db2013-12-20 11:09:51 +01001065 if (!conn_xprt_ready(srv_conn)) {
1066 /* the target was only on the session, assign it to the SI now */
1067 srv_conn->target = s->target;
Willy Tarreaud88edf22009-06-14 15:48:17 +02001068
Willy Tarreauff605db2013-12-20 11:09:51 +01001069 /* set the correct protocol on the output stream interface */
1070 if (objt_server(s->target)) {
1071 conn_prepare(srv_conn, objt_server(s->target)->proto, objt_server(s->target)->xprt);
1072 }
1073 else if (obj_type(s->target) == OBJ_TYPE_PROXY) {
1074 /* proxies exclusively run on raw_sock right now */
1075 conn_prepare(srv_conn, protocol_by_family(srv_conn->addr.to.ss_family), &raw_sock);
1076 if (!objt_conn(s->req->cons->end) || !objt_conn(s->req->cons->end)->ctrl)
1077 return SN_ERR_INTERNAL;
1078 }
1079 else
1080 return SN_ERR_INTERNAL; /* how did we get there ? */
Willy Tarreaud02394b2012-05-11 18:32:18 +02001081
Willy Tarreauff605db2013-12-20 11:09:51 +01001082 /* process the case where the server requires the PROXY protocol to be sent */
1083 srv_conn->send_proxy_ofs = 0;
David Safb76832014-05-08 23:42:08 -04001084 if (objt_server(s->target) && objt_server(s->target)->pp_opts) {
Willy Tarreauff605db2013-12-20 11:09:51 +01001085 srv_conn->send_proxy_ofs = 1; /* must compute size */
1086 cli_conn = objt_conn(s->req->prod->end);
1087 if (cli_conn)
1088 conn_get_to_addr(cli_conn);
1089 }
Willy Tarreau2a6e8802013-10-24 15:50:53 +02001090
Willy Tarreauff605db2013-12-20 11:09:51 +01001091 si_attach_conn(s->req->cons, srv_conn);
Willy Tarreau26d8c592012-05-07 18:12:14 +02001092
Willy Tarreauff605db2013-12-20 11:09:51 +01001093 assign_tproxy_address(s);
1094 }
1095 else {
1096 /* the connection is being reused, just re-attach it */
1097 si_attach_conn(s->req->cons, srv_conn);
Willy Tarreau36346242014-02-24 18:26:30 +01001098 s->flags |= SN_SRV_REUSED;
Willy Tarreauff605db2013-12-20 11:09:51 +01001099 }
Willy Tarreaub1d67742010-03-29 19:36:59 +02001100
William Lallemandb7ff6a32012-03-02 14:35:21 +01001101 /* flag for logging source ip/port */
1102 if (s->fe->options2 & PR_O2_SRC_ADDR)
1103 s->req->cons->flags |= SI_FL_SRC_ADDR;
1104
Willy Tarreau14f8e862012-08-30 22:23:13 +02001105 /* disable lingering */
1106 if (s->be->options & PR_O_TCP_NOLING)
1107 s->req->cons->flags |= SI_FL_NOLINGER;
1108
Willy Tarreau73b013b2012-05-21 16:31:45 +02001109 err = si_connect(s->req->cons);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001110
Willy Tarreau9650f372009-08-16 14:02:45 +02001111 if (err != SN_ERR_NONE)
1112 return err;
Willy Tarreau788e2842008-08-26 13:25:39 +02001113
Willy Tarreau14f8e862012-08-30 22:23:13 +02001114 /* set connect timeout */
1115 s->req->cons->exp = tick_add_ifset(now_ms, s->be->timeout.connect);
1116
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001117 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001118 if (srv) {
Willy Tarreau1e62de62008-11-11 20:20:02 +01001119 s->flags |= SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001120 srv->cur_sess++;
1121 if (srv->cur_sess > srv->counters.cur_sess_max)
1122 srv->counters.cur_sess_max = srv->cur_sess;
Willy Tarreau51406232008-03-10 22:04:20 +01001123 if (s->be->lbprm.server_take_conn)
Willy Tarreau827aee92011-03-10 16:55:02 +01001124 s->be->lbprm.server_take_conn(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001125 }
1126
Willy Tarreaubaaee002006-06-26 02:48:02 +02001127 return SN_ERR_NONE; /* connection is OK */
1128}
1129
1130
Willy Tarreaubaaee002006-06-26 02:48:02 +02001131/* This function performs the "redispatch" part of a connection attempt. It
1132 * will assign a server if required, queue the connection if required, and
1133 * handle errors that might arise at this level. It can change the server
1134 * state. It will return 1 if it encounters an error, switches the server
1135 * state, or has to queue a connection. Otherwise, it will return 0 indicating
1136 * that the connection is ready to use.
1137 */
1138
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001139int srv_redispatch_connect(struct session *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001140{
Willy Tarreau827aee92011-03-10 16:55:02 +01001141 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001142 int conn_err;
1143
1144 /* We know that we don't have any connection pending, so we will
1145 * try to get a new one, and wait in this state if it's queued
1146 */
Willy Tarreau7c669d72008-06-20 15:04:11 +02001147 redispatch:
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001148 conn_err = assign_server_and_queue(s);
1149 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001150
Willy Tarreaubaaee002006-06-26 02:48:02 +02001151 switch (conn_err) {
1152 case SRV_STATUS_OK:
1153 break;
1154
Willy Tarreau7c669d72008-06-20 15:04:11 +02001155 case SRV_STATUS_FULL:
1156 /* The server has reached its maxqueue limit. Either PR_O_REDISP is set
1157 * and we can redispatch to another server, or it is not and we return
1158 * 503. This only makes sense in DIRECT mode however, because normal LB
1159 * algorithms would never select such a server, and hash algorithms
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001160 * would bring us on the same server again. Note that s->target is set
Willy Tarreau827aee92011-03-10 16:55:02 +01001161 * in this case.
Willy Tarreau7c669d72008-06-20 15:04:11 +02001162 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001163 if (((s->flags & (SN_DIRECT|SN_FORCE_PRST)) == SN_DIRECT) &&
1164 (s->be->options & PR_O_REDISP)) {
1165 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Willy Tarreau7c669d72008-06-20 15:04:11 +02001166 goto redispatch;
1167 }
1168
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001169 if (!s->req->cons->err_type) {
1170 s->req->cons->err_type = SI_ET_QUEUE_ERR;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001171 }
Willy Tarreau7c669d72008-06-20 15:04:11 +02001172
Willy Tarreau827aee92011-03-10 16:55:02 +01001173 srv->counters.failed_conns++;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001174 s->be->be_counters.failed_conns++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02001175 return 1;
1176
Willy Tarreaubaaee002006-06-26 02:48:02 +02001177 case SRV_STATUS_NOSRV:
Willy Tarreau827aee92011-03-10 16:55:02 +01001178 /* note: it is guaranteed that srv == NULL here */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001179 if (!s->req->cons->err_type) {
1180 s->req->cons->err_type = SI_ET_CONN_ERR;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001181 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +01001182
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001183 s->be->be_counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001184 return 1;
1185
1186 case SRV_STATUS_QUEUED:
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001187 s->req->cons->exp = tick_add_ifset(now_ms, s->be->timeout.queue);
1188 s->req->cons->state = SI_ST_QUE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001189 /* do nothing else and do not wake any other session up */
1190 return 1;
1191
Willy Tarreaubaaee002006-06-26 02:48:02 +02001192 case SRV_STATUS_INTERNAL:
1193 default:
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001194 if (!s->req->cons->err_type) {
1195 s->req->cons->err_type = SI_ET_CONN_OTHER;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001196 }
1197
Willy Tarreau827aee92011-03-10 16:55:02 +01001198 if (srv)
1199 srv_inc_sess_ctr(srv);
1200 if (srv)
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001201 srv_set_sess_last(srv);
1202 if (srv)
Willy Tarreau827aee92011-03-10 16:55:02 +01001203 srv->counters.failed_conns++;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001204 s->be->be_counters.failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001205
1206 /* release other sessions waiting for this server */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001207 if (may_dequeue_tasks(srv, s->be))
Willy Tarreau827aee92011-03-10 16:55:02 +01001208 process_srv_queue(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001209 return 1;
1210 }
1211 /* if we get here, it's because we got SRV_STATUS_OK, which also
1212 * means that the connection has not been queued.
1213 */
1214 return 0;
1215}
1216
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001217/* sends a log message when a backend goes down, and also sets last
1218 * change date.
1219 */
1220void set_backend_down(struct proxy *be)
1221{
1222 be->last_change = now.tv_sec;
1223 be->down_trans++;
1224
1225 Alert("%s '%s' has no server available!\n", proxy_type_str(be), be->id);
1226 send_log(be, LOG_EMERG, "%s %s has no server available!\n", proxy_type_str(be), be->id);
1227}
1228
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001229/* Apply RDP cookie persistence to the current session. For this, the function
1230 * tries to extract an RDP cookie from the request buffer, and look for the
1231 * matching server in the list. If the server is found, it is assigned to the
1232 * session. This always returns 1, and the analyser removes itself from the
1233 * list. Nothing is performed if a server was already assigned.
1234 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02001235int tcp_persist_rdp_cookie(struct session *s, struct channel *req, int an_bit)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001236{
1237 struct proxy *px = s->be;
1238 int ret;
Willy Tarreau37406352012-04-23 16:16:37 +02001239 struct sample smp;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001240 struct server *srv = px->srv;
1241 struct sockaddr_in addr;
1242 char *p;
1243
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001244 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 +02001245 now_ms, __FUNCTION__,
1246 s,
1247 req,
1248 req->rex, req->wex,
1249 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001250 req->buf->i,
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001251 req->analysers);
1252
1253 if (s->flags & SN_ASSIGNED)
1254 goto no_cookie;
1255
Willy Tarreau37406352012-04-23 16:16:37 +02001256 memset(&smp, 0, sizeof(smp));
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001257
Willy Tarreaucadd8c92013-07-22 18:09:52 +02001258 ret = fetch_rdp_cookie_name(s, &smp, s->be->rdp_cookie_name, s->be->rdp_cookie_len);
Willy Tarreauf853c462012-04-23 18:53:56 +02001259 if (ret == 0 || (smp.flags & SMP_F_MAY_CHANGE) || smp.data.str.len == 0)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001260 goto no_cookie;
1261
1262 memset(&addr, 0, sizeof(addr));
1263 addr.sin_family = AF_INET;
1264
Willy Tarreau664092c2011-12-16 19:11:42 +01001265 /* Considering an rdp cookie detected using acl, str ended with <cr><lf> and should return */
Willy Tarreauf853c462012-04-23 18:53:56 +02001266 addr.sin_addr.s_addr = strtoul(smp.data.str.str, &p, 10);
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001267 if (*p != '.')
1268 goto no_cookie;
1269 p++;
1270 addr.sin_port = (unsigned short)strtoul(p, &p, 10);
1271 if (*p != '.')
1272 goto no_cookie;
1273
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001274 s->target = NULL;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001275 while (srv) {
Willy Tarreau28e9d062014-05-09 22:47:50 +02001276 if (srv->addr.ss_family == AF_INET &&
1277 memcmp(&addr, &(srv->addr), sizeof(addr)) == 0) {
Willy Tarreau892337c2014-05-13 23:41:20 +02001278 if ((srv->state != SRV_ST_STOPPED) || (px->options & PR_O_PERSIST)) {
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001279 /* we found the server and it is usable */
1280 s->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001281 s->target = &srv->obj_type;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02001282 break;
1283 }
1284 }
1285 srv = srv->next;
1286 }
1287
1288no_cookie:
1289 req->analysers &= ~an_bit;
1290 req->analyse_exp = TICK_ETERNITY;
1291 return 1;
1292}
1293
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001294int be_downtime(struct proxy *px) {
Willy Tarreaub625a082007-11-26 01:15:43 +01001295 if (px->lbprm.tot_weight && px->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001296 return px->down_time;
1297
1298 return now.tv_sec - px->last_change + px->down_time;
1299}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001300
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001301/*
1302 * This function returns a string containing the balancing
1303 * mode of the proxy in a format suitable for stats.
1304 */
1305
1306const char *backend_lb_algo_str(int algo) {
1307
1308 if (algo == BE_LB_ALGO_RR)
1309 return "roundrobin";
1310 else if (algo == BE_LB_ALGO_SRR)
1311 return "static-rr";
Willy Tarreauf09c6602012-02-13 17:12:08 +01001312 else if (algo == BE_LB_ALGO_FAS)
1313 return "first";
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001314 else if (algo == BE_LB_ALGO_LC)
1315 return "leastconn";
1316 else if (algo == BE_LB_ALGO_SH)
1317 return "source";
1318 else if (algo == BE_LB_ALGO_UH)
1319 return "uri";
1320 else if (algo == BE_LB_ALGO_PH)
1321 return "url_param";
1322 else if (algo == BE_LB_ALGO_HH)
1323 return "hdr";
1324 else if (algo == BE_LB_ALGO_RCH)
1325 return "rdp-cookie";
1326 else
1327 return NULL;
1328}
1329
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001330/* This function parses a "balance" statement in a backend section describing
1331 * <curproxy>. It returns -1 if there is any error, otherwise zero. If it
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001332 * returns -1, it will write an error message into the <err> buffer which will
1333 * automatically be allocated and must be passed as NULL. The trailing '\n'
1334 * will not be written. The function must be called with <args> pointing to the
1335 * first word after "balance".
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001336 */
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001337int backend_parse_balance(const char **args, char **err, struct proxy *curproxy)
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001338{
1339 if (!*(args[0])) {
1340 /* if no option is set, use round-robin by default */
Willy Tarreau31682232007-11-29 15:38:04 +01001341 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1342 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001343 return 0;
1344 }
1345
1346 if (!strcmp(args[0], "roundrobin")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001347 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1348 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001349 }
Willy Tarreau9757a382009-10-03 12:56:50 +02001350 else if (!strcmp(args[0], "static-rr")) {
1351 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1352 curproxy->lbprm.algo |= BE_LB_ALGO_SRR;
1353 }
Willy Tarreauf09c6602012-02-13 17:12:08 +01001354 else if (!strcmp(args[0], "first")) {
1355 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1356 curproxy->lbprm.algo |= BE_LB_ALGO_FAS;
1357 }
Willy Tarreau51406232008-03-10 22:04:20 +01001358 else if (!strcmp(args[0], "leastconn")) {
1359 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1360 curproxy->lbprm.algo |= BE_LB_ALGO_LC;
1361 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001362 else if (!strcmp(args[0], "source")) {
Willy Tarreau31682232007-11-29 15:38:04 +01001363 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1364 curproxy->lbprm.algo |= BE_LB_ALGO_SH;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001365 }
1366 else if (!strcmp(args[0], "uri")) {
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001367 int arg = 1;
1368
Willy Tarreau31682232007-11-29 15:38:04 +01001369 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1370 curproxy->lbprm.algo |= BE_LB_ALGO_UH;
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001371
Oskar Stolc8dc41842012-05-19 10:19:54 +01001372 curproxy->uri_whole = 0;
1373
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001374 while (*args[arg]) {
1375 if (!strcmp(args[arg], "len")) {
1376 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001377 memprintf(err, "%s : '%s' expects a positive integer (got '%s').", args[0], args[arg], args[arg+1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001378 return -1;
1379 }
1380 curproxy->uri_len_limit = atoi(args[arg+1]);
1381 arg += 2;
1382 }
1383 else if (!strcmp(args[arg], "depth")) {
1384 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001385 memprintf(err, "%s : '%s' expects a positive integer (got '%s').", args[0], args[arg], args[arg+1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001386 return -1;
1387 }
1388 /* hint: we store the position of the ending '/' (depth+1) so
1389 * that we avoid a comparison while computing the hash.
1390 */
1391 curproxy->uri_dirs_depth1 = atoi(args[arg+1]) + 1;
1392 arg += 2;
1393 }
Oskar Stolc8dc41842012-05-19 10:19:54 +01001394 else if (!strcmp(args[arg], "whole")) {
1395 curproxy->uri_whole = 1;
1396 arg += 1;
1397 }
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001398 else {
Oskar Stolc8dc41842012-05-19 10:19:54 +01001399 memprintf(err, "%s only accepts parameters 'len', 'depth', and 'whole' (got '%s').", args[0], args[arg]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001400 return -1;
1401 }
1402 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001403 }
Willy Tarreau01732802007-11-01 22:48:15 +01001404 else if (!strcmp(args[0], "url_param")) {
1405 if (!*args[1]) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001406 memprintf(err, "%s requires an URL parameter name.", args[0]);
Willy Tarreau01732802007-11-01 22:48:15 +01001407 return -1;
1408 }
Willy Tarreau31682232007-11-29 15:38:04 +01001409 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1410 curproxy->lbprm.algo |= BE_LB_ALGO_PH;
Willy Tarreaua534fea2008-08-03 12:19:50 +02001411
1412 free(curproxy->url_param_name);
Willy Tarreau01732802007-11-01 22:48:15 +01001413 curproxy->url_param_name = strdup(args[1]);
Willy Tarreaua534fea2008-08-03 12:19:50 +02001414 curproxy->url_param_len = strlen(args[1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02001415 if (*args[2]) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001416 if (strcmp(args[2], "check_post")) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001417 memprintf(err, "%s only accepts 'check_post' modifier (got '%s').", args[0], args[2]);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001418 return -1;
1419 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02001420 }
Benoitaffb4812009-03-25 13:02:10 +01001421 }
1422 else if (!strncmp(args[0], "hdr(", 4)) {
1423 const char *beg, *end;
1424
1425 beg = args[0] + 4;
1426 end = strchr(beg, ')');
1427
1428 if (!end || end == beg) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001429 memprintf(err, "hdr requires an http header field name.");
Benoitaffb4812009-03-25 13:02:10 +01001430 return -1;
1431 }
1432
1433 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1434 curproxy->lbprm.algo |= BE_LB_ALGO_HH;
1435
1436 free(curproxy->hh_name);
1437 curproxy->hh_len = end - beg;
1438 curproxy->hh_name = my_strndup(beg, end - beg);
1439 curproxy->hh_match_domain = 0;
1440
1441 if (*args[1]) {
1442 if (strcmp(args[1], "use_domain_only")) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001443 memprintf(err, "%s only accepts 'use_domain_only' modifier (got '%s').", args[0], args[1]);
Benoitaffb4812009-03-25 13:02:10 +01001444 return -1;
1445 }
1446 curproxy->hh_match_domain = 1;
1447 }
Emeric Brun736aa232009-06-30 17:56:00 +02001448 }
1449 else if (!strncmp(args[0], "rdp-cookie", 10)) {
1450 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1451 curproxy->lbprm.algo |= BE_LB_ALGO_RCH;
1452
1453 if ( *(args[0] + 10 ) == '(' ) { /* cookie name */
1454 const char *beg, *end;
1455
1456 beg = args[0] + 11;
1457 end = strchr(beg, ')');
1458
1459 if (!end || end == beg) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001460 memprintf(err, "rdp-cookie : missing cookie name.");
Emeric Brun736aa232009-06-30 17:56:00 +02001461 return -1;
1462 }
1463
1464 free(curproxy->hh_name);
1465 curproxy->hh_name = my_strndup(beg, end - beg);
1466 curproxy->hh_len = end - beg;
1467 }
1468 else if ( *(args[0] + 10 ) == '\0' ) { /* default cookie name 'mstshash' */
1469 free(curproxy->hh_name);
1470 curproxy->hh_name = strdup("mstshash");
1471 curproxy->hh_len = strlen(curproxy->hh_name);
1472 }
1473 else { /* syntax */
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001474 memprintf(err, "rdp-cookie : missing cookie name.");
Emeric Brun736aa232009-06-30 17:56:00 +02001475 return -1;
1476 }
Willy Tarreau01732802007-11-01 22:48:15 +01001477 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01001478 else {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02001479 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 +01001480 return -1;
1481 }
1482 return 0;
1483}
1484
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001485
1486/************************************************************************/
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001487/* All supported sample and ACL keywords must be declared here. */
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001488/************************************************************************/
1489
Willy Tarreau34db1082012-04-19 17:16:54 +02001490/* set temp integer to the number of enabled servers on the proxy.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001491 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001492 * undefined behaviour.
1493 */
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001494static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001495smp_fetch_nbsrv(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001496 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001497{
Willy Tarreau37406352012-04-23 16:16:37 +02001498 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001499 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001500 px = args->data.prx;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001501
1502 if (px->srv_act)
Willy Tarreauf853c462012-04-23 18:53:56 +02001503 smp->data.uint = px->srv_act;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001504 else if (px->lbprm.fbck)
Willy Tarreauf853c462012-04-23 18:53:56 +02001505 smp->data.uint = 1;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001506 else
Willy Tarreauf853c462012-04-23 18:53:56 +02001507 smp->data.uint = px->srv_bck;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001508
1509 return 1;
1510}
1511
Willy Tarreau37406352012-04-23 16:16:37 +02001512/* report in smp->flags a success or failure depending on the designated
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001513 * server's state. There is no match function involved since there's no pattern.
Willy Tarreau34db1082012-04-19 17:16:54 +02001514 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1515 * undefined behaviour.
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001516 */
1517static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001518smp_fetch_srv_is_up(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001519 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001520{
Willy Tarreau24e32d82012-04-23 23:55:44 +02001521 struct server *srv = args->data.srv;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001522
Willy Tarreau37406352012-04-23 16:16:37 +02001523 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001524 smp->type = SMP_T_BOOL;
Willy Tarreau20125212014-05-13 19:44:56 +02001525 if (!(srv->admin & SRV_ADMF_MAINT) &&
Willy Tarreau892337c2014-05-13 23:41:20 +02001526 (!(srv->check.state & CHK_ST_CONFIGURED) || (srv->state != SRV_ST_STOPPED)))
Willy Tarreau197e10a2012-04-23 19:18:42 +02001527 smp->data.uint = 1;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001528 else
Willy Tarreau197e10a2012-04-23 19:18:42 +02001529 smp->data.uint = 0;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02001530 return 1;
1531}
1532
Willy Tarreau34db1082012-04-19 17:16:54 +02001533/* set temp integer to the number of enabled servers on the proxy.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001534 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001535 * undefined behaviour.
1536 */
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001537static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001538smp_fetch_connslots(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001539 const struct arg *args, struct sample *smp, const char *kw)
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001540{
1541 struct server *iterator;
Willy Tarreaud28c3532012-04-19 19:28:33 +02001542
Willy Tarreau37406352012-04-23 16:16:37 +02001543 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001544 smp->type = SMP_T_UINT;
1545 smp->data.uint = 0;
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001546
Willy Tarreau24e32d82012-04-23 23:55:44 +02001547 for (iterator = args->data.prx->srv; iterator; iterator = iterator->next) {
Willy Tarreau892337c2014-05-13 23:41:20 +02001548 if (iterator->state == SRV_ST_STOPPED)
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001549 continue;
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001550
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001551 if (iterator->maxconn == 0 || iterator->maxqueue == 0) {
Willy Tarreaua5e37562011-12-16 17:06:15 +01001552 /* configuration is stupid */
Willy Tarreauf853c462012-04-23 18:53:56 +02001553 smp->data.uint = -1; /* FIXME: stupid value! */
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001554 return 1;
1555 }
1556
Willy Tarreauf853c462012-04-23 18:53:56 +02001557 smp->data.uint += (iterator->maxconn - iterator->cur_sess)
Willy Tarreau422aa072012-04-20 20:49:27 +02001558 + (iterator->maxqueue - iterator->nbpend);
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08001559 }
1560
1561 return 1;
1562}
1563
Willy Tarreaua5e37562011-12-16 17:06:15 +01001564/* set temp integer to the id of the backend */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001565static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001566smp_fetch_be_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 Tarreauf853c462012-04-23 18:53:56 +02001569 smp->flags = SMP_F_VOL_TXN;
1570 smp->type = SMP_T_UINT;
1571 smp->data.uint = l4->be->uuid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001572 return 1;
1573}
1574
Willy Tarreaua5e37562011-12-16 17:06:15 +01001575/* set temp integer to the id of the server */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001576static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001577smp_fetch_srv_id(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001578 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau37406352012-04-23 16:16:37 +02001579{
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001580 if (!objt_server(l4->target))
Willy Tarreau17af4192011-02-23 14:27:06 +01001581 return 0;
1582
Willy Tarreauf853c462012-04-23 18:53:56 +02001583 smp->type = SMP_T_UINT;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001584 smp->data.uint = objt_server(l4->target)->puid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01001585
1586 return 1;
1587}
1588
Willy Tarreau34db1082012-04-19 17:16:54 +02001589/* set temp integer to the number of connections per second reaching the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001590 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001591 * undefined behaviour.
1592 */
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001593static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001594smp_fetch_be_sess_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001595 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001596{
Willy Tarreau37406352012-04-23 16:16:37 +02001597 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001598 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001599 smp->data.uint = read_freq_ctr(&args->data.prx->be_sess_per_sec);
Willy Tarreau079ff0a2009-03-05 21:34:28 +01001600 return 1;
1601}
1602
Willy Tarreau34db1082012-04-19 17:16:54 +02001603/* set temp integer to the number of concurrent connections on the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001604 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001605 * undefined behaviour.
1606 */
Willy Tarreaua36af912009-10-10 12:02:45 +02001607static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001608smp_fetch_be_conn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001609 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua36af912009-10-10 12:02:45 +02001610{
Willy Tarreau37406352012-04-23 16:16:37 +02001611 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001612 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001613 smp->data.uint = args->data.prx->beconn;
Willy Tarreaua36af912009-10-10 12:02:45 +02001614 return 1;
1615}
1616
Willy Tarreau34db1082012-04-19 17:16:54 +02001617/* set temp integer to the total number of queued connections on the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001618 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001619 * undefined behaviour.
1620 */
Willy Tarreaua36af912009-10-10 12:02:45 +02001621static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001622smp_fetch_queue_size(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001623 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua36af912009-10-10 12:02:45 +02001624{
Willy Tarreau37406352012-04-23 16:16:37 +02001625 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001626 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001627 smp->data.uint = args->data.prx->totpend;
Willy Tarreaua36af912009-10-10 12:02:45 +02001628 return 1;
1629}
1630
Willy Tarreaua5e37562011-12-16 17:06:15 +01001631/* set temp integer to the total number of queued connections on the backend divided
Willy Tarreaua36af912009-10-10 12:02:45 +02001632 * by the number of running servers and rounded up. If there is no running
1633 * server, we return twice the total, just as if we had half a running server.
1634 * This is more or less correct anyway, since we expect the last server to come
1635 * back soon.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02001636 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02001637 * undefined behaviour.
Willy Tarreaua36af912009-10-10 12:02:45 +02001638 */
1639static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001640smp_fetch_avg_queue_size(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001641 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua36af912009-10-10 12:02:45 +02001642{
1643 int nbsrv;
1644
Willy Tarreau37406352012-04-23 16:16:37 +02001645 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02001646 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001647 px = args->data.prx;
Willy Tarreaua36af912009-10-10 12:02:45 +02001648
1649 if (px->srv_act)
1650 nbsrv = px->srv_act;
1651 else if (px->lbprm.fbck)
1652 nbsrv = 1;
1653 else
1654 nbsrv = px->srv_bck;
1655
1656 if (nbsrv > 0)
Willy Tarreauf853c462012-04-23 18:53:56 +02001657 smp->data.uint = (px->totpend + nbsrv - 1) / nbsrv;
Willy Tarreaua36af912009-10-10 12:02:45 +02001658 else
Willy Tarreauf853c462012-04-23 18:53:56 +02001659 smp->data.uint = px->totpend * 2;
Willy Tarreaua36af912009-10-10 12:02:45 +02001660
1661 return 1;
1662}
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001663
Willy Tarreau34db1082012-04-19 17:16:54 +02001664/* set temp integer to the number of concurrent connections on the server in the backend.
1665 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1666 * undefined behaviour.
1667 */
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001668static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001669smp_fetch_srv_conn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001670 const struct arg *args, struct sample *smp, const char *kw)
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001671{
Willy Tarreauf853c462012-04-23 18:53:56 +02001672 smp->flags = SMP_F_VOL_TEST;
1673 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02001674 smp->data.uint = args->data.srv->cur_sess;
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02001675 return 1;
1676}
1677
Tait Clarridge7896d522012-12-05 21:39:31 -05001678/* set temp integer to the number of enabled servers on the proxy.
1679 * Accepts exactly 1 argument. Argument is a server, other types will lead to
1680 * undefined behaviour.
1681 */
1682static int
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001683smp_fetch_srv_sess_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001684 const struct arg *args, struct sample *smp, const char *kw)
Tait Clarridge7896d522012-12-05 21:39:31 -05001685{
1686 smp->flags = SMP_F_VOL_TEST;
1687 smp->type = SMP_T_UINT;
1688 smp->data.uint = read_freq_ctr(&args->data.srv->sess_per_sec);
1689 return 1;
1690}
1691
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001692
1693/* Note: must not be declared <const> as its list will be overwritten.
1694 * Please take care of keeping this list alphabetically sorted.
1695 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001696static struct sample_fetch_kw_list smp_kws = {ILH, {
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001697 { "avg_queue", smp_fetch_avg_queue_size, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1698 { "be_conn", smp_fetch_be_conn, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1699 { "be_id", smp_fetch_be_id, 0, NULL, SMP_T_UINT, SMP_USE_BKEND, },
1700 { "be_sess_rate", smp_fetch_be_sess_rate, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1701 { "connslots", smp_fetch_connslots, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1702 { "nbsrv", smp_fetch_nbsrv, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1703 { "queue", smp_fetch_queue_size, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1704 { "srv_conn", smp_fetch_srv_conn, ARG1(1,SRV), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1705 { "srv_id", smp_fetch_srv_id, 0, NULL, SMP_T_UINT, SMP_USE_SERVR, },
1706 { "srv_is_up", smp_fetch_srv_is_up, ARG1(1,SRV), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
1707 { "srv_sess_rate", smp_fetch_srv_sess_rate, ARG1(1,SRV), NULL, SMP_T_UINT, SMP_USE_INTRN, },
1708 { /* END */ },
1709}};
1710
1711
Willy Tarreau61612d42012-04-19 18:42:05 +02001712/* Note: must not be declared <const> as its list will be overwritten.
1713 * Please take care of keeping this list alphabetically sorted.
1714 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001715static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001716 { /* END */ },
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001717}};
1718
1719
1720__attribute__((constructor))
1721static void __backend_init(void)
1722{
Willy Tarreau1a7eca12013-01-07 22:38:03 +01001723 sample_register_fetches(&smp_kws);
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01001724 acl_register_keywords(&acl_kws);
1725}
1726
Willy Tarreaubaaee002006-06-26 02:48:02 +02001727/*
1728 * Local variables:
1729 * c-indent-level: 8
1730 * c-basic-offset: 8
1731 * End:
1732 */