Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Backend variables and functions. |
| 3 | * |
Willy Tarreau | 1a7eca1 | 2013-01-07 22:38:03 +0100 | [diff] [blame] | 4 | * Copyright 2000-2013 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5 | * |
| 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 Tarreau | f19cf37 | 2006-11-14 15:40:51 +0100 | [diff] [blame] | 18 | #include <string.h> |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 19 | #include <ctype.h> |
Dmitry Sivachenko | caf5898 | 2009-08-24 15:11:06 +0400 | [diff] [blame] | 20 | #include <sys/types.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 21 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 22 | #include <common/buffer.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 23 | #include <common/compat.h> |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 24 | #include <common/config.h> |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 25 | #include <common/debug.h> |
Bhaskar | 98634f0 | 2013-10-29 23:30:51 -0400 | [diff] [blame] | 26 | #include <common/hash.h> |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 27 | #include <common/ticks.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 28 | #include <common/time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 29 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 30 | #include <types/global.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 31 | |
Willy Tarreau | a9d3c1e | 2007-11-30 20:48:53 +0100 | [diff] [blame] | 32 | #include <proto/acl.h> |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 33 | #include <proto/arg.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 34 | #include <proto/backend.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 35 | #include <proto/channel.h> |
Willy Tarreau | 03fa5df | 2010-05-24 21:02:37 +0200 | [diff] [blame] | 36 | #include <proto/frontend.h> |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 37 | #include <proto/lb_chash.h> |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 38 | #include <proto/lb_fas.h> |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 39 | #include <proto/lb_fwlc.h> |
| 40 | #include <proto/lb_fwrr.h> |
| 41 | #include <proto/lb_map.h> |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 42 | #include <proto/obj_type.h> |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 43 | #include <proto/payload.h> |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 44 | #include <proto/protocol.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 45 | #include <proto/proto_http.h> |
Willy Tarreau | a8f55d5 | 2010-05-31 17:44:19 +0200 | [diff] [blame] | 46 | #include <proto/proto_tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 47 | #include <proto/queue.h> |
Willy Tarreau | d4c33c8 | 2013-01-07 21:59:07 +0100 | [diff] [blame] | 48 | #include <proto/sample.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 49 | #include <proto/server.h> |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 50 | #include <proto/session.h> |
Willy Tarreau | 75bf2c9 | 2012-08-20 17:01:35 +0200 | [diff] [blame] | 51 | #include <proto/raw_sock.h> |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 52 | #include <proto/stream_interface.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 53 | #include <proto/task.h> |
| 54 | |
Bhaskar Maddala | a20cb85 | 2014-02-03 16:26:46 -0500 | [diff] [blame] | 55 | int be_lastsession(const struct proxy *be) |
| 56 | { |
| 57 | if (be->be_counters.last_sess) |
| 58 | return now.tv_sec - be->be_counters.last_sess; |
| 59 | |
| 60 | return -1; |
| 61 | } |
| 62 | |
Bhaskar | 98634f0 | 2013-10-29 23:30:51 -0400 | [diff] [blame] | 63 | /* helper function to invoke the correct hash method */ |
| 64 | static unsigned long gen_hash(const struct proxy* px, const char* key, unsigned long len) |
| 65 | { |
| 66 | unsigned long hash; |
| 67 | |
| 68 | switch (px->lbprm.algo & BE_LB_HASH_FUNC) { |
| 69 | case BE_LB_HFCN_DJB2: |
| 70 | hash = hash_djb2(key, len); |
| 71 | break; |
Willy Tarreau | a0f4271 | 2013-11-14 14:30:35 +0100 | [diff] [blame] | 72 | case BE_LB_HFCN_WT6: |
| 73 | hash = hash_wt6(key, len); |
| 74 | break; |
Bhaskar | 98634f0 | 2013-10-29 23:30:51 -0400 | [diff] [blame] | 75 | case BE_LB_HFCN_SDBM: |
| 76 | /* this is the default hash function */ |
| 77 | default: |
| 78 | hash = hash_sdbm(key, len); |
| 79 | break; |
| 80 | } |
| 81 | |
| 82 | return hash; |
| 83 | } |
| 84 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 85 | /* |
| 86 | * This function recounts the number of usable active and backup servers for |
| 87 | * proxy <p>. These numbers are returned into the p->srv_act and p->srv_bck. |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 88 | * This function also recomputes the total active and backup weights. However, |
Willy Tarreau | f4cca45 | 2008-03-08 21:42:54 +0100 | [diff] [blame] | 89 | * it does not update tot_weight nor tot_used. Use update_backend_weight() for |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 90 | * this. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 91 | */ |
Willy Tarreau | c5d9c80 | 2009-10-01 09:17:05 +0200 | [diff] [blame] | 92 | void recount_servers(struct proxy *px) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 93 | { |
| 94 | struct server *srv; |
| 95 | |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 96 | px->srv_act = px->srv_bck = 0; |
| 97 | px->lbprm.tot_wact = px->lbprm.tot_wbck = 0; |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 98 | px->lbprm.fbck = NULL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 99 | for (srv = px->srv; srv != NULL; srv = srv->next) { |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 100 | if (!srv_is_usable(srv->state, srv->eweight)) |
| 101 | continue; |
| 102 | |
| 103 | if (srv->state & SRV_BACKUP) { |
| 104 | if (!px->srv_bck && |
Willy Tarreau | f4cca45 | 2008-03-08 21:42:54 +0100 | [diff] [blame] | 105 | !(px->options & PR_O_USE_ALL_BK)) |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 106 | px->lbprm.fbck = srv; |
| 107 | px->srv_bck++; |
| 108 | px->lbprm.tot_wbck += srv->eweight; |
| 109 | } else { |
| 110 | px->srv_act++; |
| 111 | px->lbprm.tot_wact += srv->eweight; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 112 | } |
| 113 | } |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 114 | } |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 115 | |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 116 | /* This function simply updates the backend's tot_weight and tot_used values |
| 117 | * after servers weights have been updated. It is designed to be used after |
| 118 | * recount_servers() or equivalent. |
| 119 | */ |
Willy Tarreau | c5d9c80 | 2009-10-01 09:17:05 +0200 | [diff] [blame] | 120 | void update_backend_weight(struct proxy *px) |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 121 | { |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 122 | if (px->srv_act) { |
| 123 | px->lbprm.tot_weight = px->lbprm.tot_wact; |
| 124 | px->lbprm.tot_used = px->srv_act; |
| 125 | } |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 126 | else if (px->lbprm.fbck) { |
| 127 | /* use only the first backup server */ |
| 128 | px->lbprm.tot_weight = px->lbprm.fbck->eweight; |
| 129 | px->lbprm.tot_used = 1; |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 130 | } |
| 131 | else { |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 132 | px->lbprm.tot_weight = px->lbprm.tot_wbck; |
| 133 | px->lbprm.tot_used = px->srv_bck; |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 134 | } |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 135 | } |
| 136 | |
Willy Tarreau | 39c9ba7 | 2009-10-01 21:11:15 +0200 | [diff] [blame] | 137 | /* |
| 138 | * This function tries to find a running server for the proxy <px> following |
| 139 | * the source hash method. Depending on the number of active/backup servers, |
| 140 | * it will either look for active servers, or for backup servers. |
| 141 | * If any server is found, it will be returned. If no valid server is found, |
| 142 | * NULL is returned. |
| 143 | */ |
| 144 | struct server *get_server_sh(struct proxy *px, const char *addr, int len) |
| 145 | { |
| 146 | unsigned int h, l; |
| 147 | |
| 148 | if (px->lbprm.tot_weight == 0) |
| 149 | return NULL; |
| 150 | |
| 151 | l = h = 0; |
| 152 | |
| 153 | /* note: we won't hash if there's only one server left */ |
| 154 | if (px->lbprm.tot_used == 1) |
| 155 | goto hash_done; |
| 156 | |
| 157 | while ((l + sizeof (int)) <= len) { |
| 158 | h ^= ntohl(*(unsigned int *)(&addr[l])); |
| 159 | l += sizeof (int); |
| 160 | } |
Bhaskar Maddala | b6c0ac9 | 2013-11-05 11:54:02 -0500 | [diff] [blame] | 161 | if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL) |
Willy Tarreau | 798a39c | 2010-11-24 15:04:29 +0100 | [diff] [blame] | 162 | h = full_hash(h); |
Willy Tarreau | 39c9ba7 | 2009-10-01 21:11:15 +0200 | [diff] [blame] | 163 | hash_done: |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 164 | if (px->lbprm.algo & BE_LB_LKUP_CHTREE) |
| 165 | return chash_get_server_hash(px, h); |
| 166 | else |
| 167 | return map_get_server_hash(px, h); |
Willy Tarreau | 39c9ba7 | 2009-10-01 21:11:15 +0200 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | /* |
| 171 | * This function tries to find a running server for the proxy <px> following |
| 172 | * the URI hash method. In order to optimize cache hits, the hash computation |
| 173 | * ends at the question mark. Depending on the number of active/backup servers, |
| 174 | * it will either look for active servers, or for backup servers. |
| 175 | * If any server is found, it will be returned. If no valid server is found, |
| 176 | * NULL is returned. |
| 177 | * |
| 178 | * This code was contributed by Guillaume Dallaire, who also selected this hash |
| 179 | * algorithm out of a tens because it gave him the best results. |
| 180 | * |
| 181 | */ |
| 182 | struct server *get_server_uh(struct proxy *px, char *uri, int uri_len) |
| 183 | { |
| 184 | unsigned long hash = 0; |
| 185 | int c; |
| 186 | int slashes = 0; |
Bhaskar | 98634f0 | 2013-10-29 23:30:51 -0400 | [diff] [blame] | 187 | const char *start, *end; |
Willy Tarreau | 39c9ba7 | 2009-10-01 21:11:15 +0200 | [diff] [blame] | 188 | |
| 189 | if (px->lbprm.tot_weight == 0) |
| 190 | return NULL; |
| 191 | |
| 192 | /* note: we won't hash if there's only one server left */ |
| 193 | if (px->lbprm.tot_used == 1) |
| 194 | goto hash_done; |
| 195 | |
| 196 | if (px->uri_len_limit) |
| 197 | uri_len = MIN(uri_len, px->uri_len_limit); |
| 198 | |
Bhaskar | 98634f0 | 2013-10-29 23:30:51 -0400 | [diff] [blame] | 199 | start = end = uri; |
Willy Tarreau | 39c9ba7 | 2009-10-01 21:11:15 +0200 | [diff] [blame] | 200 | while (uri_len--) { |
Bhaskar | 98634f0 | 2013-10-29 23:30:51 -0400 | [diff] [blame] | 201 | c = *end++; |
Willy Tarreau | 39c9ba7 | 2009-10-01 21:11:15 +0200 | [diff] [blame] | 202 | if (c == '/') { |
| 203 | slashes++; |
| 204 | if (slashes == px->uri_dirs_depth1) /* depth+1 */ |
| 205 | break; |
| 206 | } |
Oskar Stolc | 8dc4184 | 2012-05-19 10:19:54 +0100 | [diff] [blame] | 207 | else if (c == '?' && !px->uri_whole) |
Willy Tarreau | 39c9ba7 | 2009-10-01 21:11:15 +0200 | [diff] [blame] | 208 | break; |
Willy Tarreau | 39c9ba7 | 2009-10-01 21:11:15 +0200 | [diff] [blame] | 209 | } |
Bhaskar | 98634f0 | 2013-10-29 23:30:51 -0400 | [diff] [blame] | 210 | |
| 211 | hash = gen_hash(px, start, (end - start)); |
| 212 | |
Bhaskar Maddala | b6c0ac9 | 2013-11-05 11:54:02 -0500 | [diff] [blame] | 213 | if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL) |
Willy Tarreau | 798a39c | 2010-11-24 15:04:29 +0100 | [diff] [blame] | 214 | hash = full_hash(hash); |
Willy Tarreau | 39c9ba7 | 2009-10-01 21:11:15 +0200 | [diff] [blame] | 215 | hash_done: |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 216 | if (px->lbprm.algo & BE_LB_LKUP_CHTREE) |
| 217 | return chash_get_server_hash(px, hash); |
| 218 | else |
| 219 | return map_get_server_hash(px, hash); |
Willy Tarreau | 39c9ba7 | 2009-10-01 21:11:15 +0200 | [diff] [blame] | 220 | } |
| 221 | |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 222 | /* |
| 223 | * This function tries to find a running server for the proxy <px> following |
| 224 | * the URL parameter hash method. It looks for a specific parameter in the |
| 225 | * URL and hashes it to compute the server ID. This is useful to optimize |
| 226 | * performance by avoiding bounces between servers in contexts where sessions |
| 227 | * are shared but cookies are not usable. If the parameter is not found, NULL |
| 228 | * is returned. If any server is found, it will be returned. If no valid server |
| 229 | * is found, NULL is returned. |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 230 | */ |
| 231 | struct server *get_server_ph(struct proxy *px, const char *uri, int uri_len) |
| 232 | { |
| 233 | unsigned long hash = 0; |
Bhaskar | 98634f0 | 2013-10-29 23:30:51 -0400 | [diff] [blame] | 234 | const char *start, *end; |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 235 | const char *p; |
| 236 | const char *params; |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 237 | int plen; |
| 238 | |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 239 | /* when tot_weight is 0 then so is srv_count */ |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 240 | if (px->lbprm.tot_weight == 0) |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 241 | return NULL; |
| 242 | |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 243 | if ((p = memchr(uri, '?', uri_len)) == NULL) |
| 244 | return NULL; |
| 245 | |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 246 | p++; |
| 247 | |
| 248 | uri_len -= (p - uri); |
| 249 | plen = px->url_param_len; |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 250 | params = p; |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 251 | |
| 252 | while (uri_len > plen) { |
| 253 | /* Look for the parameter name followed by an equal symbol */ |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 254 | if (params[plen] == '=') { |
| 255 | if (memcmp(params, px->url_param_name, plen) == 0) { |
| 256 | /* OK, we have the parameter here at <params>, and |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 257 | * the value after the equal sign, at <p> |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 258 | * skip the equal symbol |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 259 | */ |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 260 | p += plen + 1; |
Bhaskar | 98634f0 | 2013-10-29 23:30:51 -0400 | [diff] [blame] | 261 | start = end = p; |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 262 | uri_len -= plen + 1; |
| 263 | |
Bhaskar | 98634f0 | 2013-10-29 23:30:51 -0400 | [diff] [blame] | 264 | while (uri_len && *end != '&') { |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 265 | uri_len--; |
Bhaskar | 98634f0 | 2013-10-29 23:30:51 -0400 | [diff] [blame] | 266 | end++; |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 267 | } |
Bhaskar | 98634f0 | 2013-10-29 23:30:51 -0400 | [diff] [blame] | 268 | hash = gen_hash(px, start, (end - start)); |
| 269 | |
Bhaskar Maddala | b6c0ac9 | 2013-11-05 11:54:02 -0500 | [diff] [blame] | 270 | if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL) |
Willy Tarreau | 798a39c | 2010-11-24 15:04:29 +0100 | [diff] [blame] | 271 | hash = full_hash(hash); |
Bhaskar | 98634f0 | 2013-10-29 23:30:51 -0400 | [diff] [blame] | 272 | |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 273 | if (px->lbprm.algo & BE_LB_LKUP_CHTREE) |
| 274 | return chash_get_server_hash(px, hash); |
| 275 | else |
| 276 | return map_get_server_hash(px, hash); |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 277 | } |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 278 | } |
| 279 | /* skip to next parameter */ |
| 280 | p = memchr(params, '&', uri_len); |
| 281 | if (!p) |
| 282 | return NULL; |
| 283 | p++; |
| 284 | uri_len -= (p - params); |
| 285 | params = p; |
| 286 | } |
| 287 | return NULL; |
| 288 | } |
| 289 | |
| 290 | /* |
| 291 | * this does the same as the previous server_ph, but check the body contents |
| 292 | */ |
| 293 | struct server *get_server_ph_post(struct session *s) |
| 294 | { |
| 295 | unsigned long hash = 0; |
| 296 | struct http_txn *txn = &s->txn; |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 297 | struct channel *req = s->req; |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 298 | struct http_msg *msg = &txn->req; |
| 299 | struct proxy *px = s->be; |
| 300 | unsigned int plen = px->url_param_len; |
Willy Tarreau | 2d8e485 | 2014-04-17 20:08:17 +0200 | [diff] [blame] | 301 | unsigned long len = http_body_bytes(msg); |
Willy Tarreau | 211cdec | 2014-04-17 20:18:08 +0200 | [diff] [blame] | 302 | const char *params = b_ptr(req->buf, (int)(msg->sov + msg->sol - http_hdr_rewind(msg))); |
Willy Tarreau | 157dd63 | 2009-12-06 19:18:09 +0100 | [diff] [blame] | 303 | const char *p = params; |
Bhaskar | 98634f0 | 2013-10-29 23:30:51 -0400 | [diff] [blame] | 304 | const char *start, *end; |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 305 | |
Willy Tarreau | 157dd63 | 2009-12-06 19:18:09 +0100 | [diff] [blame] | 306 | if (len == 0) |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 307 | return NULL; |
| 308 | |
Willy Tarreau | 157dd63 | 2009-12-06 19:18:09 +0100 | [diff] [blame] | 309 | if (px->lbprm.tot_weight == 0) |
| 310 | return NULL; |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 311 | |
| 312 | while (len > plen) { |
| 313 | /* Look for the parameter name followed by an equal symbol */ |
| 314 | if (params[plen] == '=') { |
| 315 | if (memcmp(params, px->url_param_name, plen) == 0) { |
| 316 | /* OK, we have the parameter here at <params>, and |
| 317 | * the value after the equal sign, at <p> |
| 318 | * skip the equal symbol |
| 319 | */ |
| 320 | p += plen + 1; |
Bhaskar | 98634f0 | 2013-10-29 23:30:51 -0400 | [diff] [blame] | 321 | start = end = p; |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 322 | len -= plen + 1; |
| 323 | |
Bhaskar | 98634f0 | 2013-10-29 23:30:51 -0400 | [diff] [blame] | 324 | while (len && *end != '&') { |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 325 | if (unlikely(!HTTP_IS_TOKEN(*p))) { |
Willy Tarreau | 157dd63 | 2009-12-06 19:18:09 +0100 | [diff] [blame] | 326 | /* if in a POST, body must be URI encoded or it's not a URI. |
Bhaskar | 98634f0 | 2013-10-29 23:30:51 -0400 | [diff] [blame] | 327 | * Do not interpret any possible binary data as a parameter. |
Willy Tarreau | 157dd63 | 2009-12-06 19:18:09 +0100 | [diff] [blame] | 328 | */ |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 329 | if (likely(HTTP_IS_LWS(*p))) /* eol, uncertain uri len */ |
| 330 | break; |
| 331 | return NULL; /* oh, no; this is not uri-encoded. |
| 332 | * This body does not contain parameters. |
| 333 | */ |
| 334 | } |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 335 | len--; |
Bhaskar | 98634f0 | 2013-10-29 23:30:51 -0400 | [diff] [blame] | 336 | end++; |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 337 | /* should we break if vlen exceeds limit? */ |
| 338 | } |
Bhaskar | 98634f0 | 2013-10-29 23:30:51 -0400 | [diff] [blame] | 339 | hash = gen_hash(px, start, (end - start)); |
| 340 | |
Bhaskar Maddala | b6c0ac9 | 2013-11-05 11:54:02 -0500 | [diff] [blame] | 341 | if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL) |
Willy Tarreau | 798a39c | 2010-11-24 15:04:29 +0100 | [diff] [blame] | 342 | hash = full_hash(hash); |
Bhaskar | 98634f0 | 2013-10-29 23:30:51 -0400 | [diff] [blame] | 343 | |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 344 | if (px->lbprm.algo & BE_LB_LKUP_CHTREE) |
| 345 | return chash_get_server_hash(px, hash); |
| 346 | else |
| 347 | return map_get_server_hash(px, hash); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 348 | } |
| 349 | } |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 350 | /* skip to next parameter */ |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 351 | p = memchr(params, '&', len); |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 352 | if (!p) |
| 353 | return NULL; |
| 354 | p++; |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 355 | len -= (p - params); |
| 356 | params = p; |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 357 | } |
| 358 | return NULL; |
| 359 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 360 | |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 361 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 362 | /* |
Benoit | affb481 | 2009-03-25 13:02:10 +0100 | [diff] [blame] | 363 | * This function tries to find a running server for the proxy <px> following |
| 364 | * the Header parameter hash method. It looks for a specific parameter in the |
| 365 | * URL and hashes it to compute the server ID. This is useful to optimize |
| 366 | * performance by avoiding bounces between servers in contexts where sessions |
| 367 | * are shared but cookies are not usable. If the parameter is not found, NULL |
| 368 | * is returned. If any server is found, it will be returned. If no valid server |
| 369 | * is found, NULL is returned. |
| 370 | */ |
| 371 | struct server *get_server_hh(struct session *s) |
| 372 | { |
| 373 | unsigned long hash = 0; |
| 374 | struct http_txn *txn = &s->txn; |
Benoit | affb481 | 2009-03-25 13:02:10 +0100 | [diff] [blame] | 375 | struct proxy *px = s->be; |
| 376 | unsigned int plen = px->hh_len; |
| 377 | unsigned long len; |
| 378 | struct hdr_ctx ctx; |
| 379 | const char *p; |
Bhaskar | 98634f0 | 2013-10-29 23:30:51 -0400 | [diff] [blame] | 380 | const char *start, *end; |
Benoit | affb481 | 2009-03-25 13:02:10 +0100 | [diff] [blame] | 381 | |
| 382 | /* tot_weight appears to mean srv_count */ |
| 383 | if (px->lbprm.tot_weight == 0) |
| 384 | return NULL; |
| 385 | |
Benoit | affb481 | 2009-03-25 13:02:10 +0100 | [diff] [blame] | 386 | ctx.idx = 0; |
| 387 | |
| 388 | /* if the message is chunked, we skip the chunk size, but use the value as len */ |
Willy Tarreau | 211cdec | 2014-04-17 20:18:08 +0200 | [diff] [blame] | 389 | http_find_header2(px->hh_name, plen, b_ptr(s->req->buf, -http_hdr_rewind(&txn->req)), &txn->hdr_idx, &ctx); |
Benoit | affb481 | 2009-03-25 13:02:10 +0100 | [diff] [blame] | 390 | |
| 391 | /* if the header is not found or empty, let's fallback to round robin */ |
| 392 | if (!ctx.idx || !ctx.vlen) |
| 393 | return NULL; |
| 394 | |
Willy Tarreau | 39c9ba7 | 2009-10-01 21:11:15 +0200 | [diff] [blame] | 395 | /* note: we won't hash if there's only one server left */ |
| 396 | if (px->lbprm.tot_used == 1) |
| 397 | goto hash_done; |
| 398 | |
Benoit | affb481 | 2009-03-25 13:02:10 +0100 | [diff] [blame] | 399 | /* Found a the hh_name in the headers. |
| 400 | * we will compute the hash based on this value ctx.val. |
| 401 | */ |
| 402 | len = ctx.vlen; |
| 403 | p = (char *)ctx.line + ctx.val; |
| 404 | if (!px->hh_match_domain) { |
Bhaskar | 98634f0 | 2013-10-29 23:30:51 -0400 | [diff] [blame] | 405 | hash = gen_hash(px, p, len); |
Benoit | affb481 | 2009-03-25 13:02:10 +0100 | [diff] [blame] | 406 | } else { |
| 407 | int dohash = 0; |
| 408 | p += len - 1; |
Bhaskar | 98634f0 | 2013-10-29 23:30:51 -0400 | [diff] [blame] | 409 | start = end = p; |
Benoit | affb481 | 2009-03-25 13:02:10 +0100 | [diff] [blame] | 410 | /* special computation, use only main domain name, not tld/host |
| 411 | * going back from the end of string, start hashing at first |
| 412 | * dot stop at next. |
| 413 | * This is designed to work with the 'Host' header, and requires |
| 414 | * a special option to activate this. |
| 415 | */ |
| 416 | while (len) { |
| 417 | if (*p == '.') { |
Bhaskar | 98634f0 | 2013-10-29 23:30:51 -0400 | [diff] [blame] | 418 | if (!dohash) { |
Benoit | affb481 | 2009-03-25 13:02:10 +0100 | [diff] [blame] | 419 | dohash = 1; |
Bhaskar | 98634f0 | 2013-10-29 23:30:51 -0400 | [diff] [blame] | 420 | start = end = p - 1; |
| 421 | } |
Benoit | affb481 | 2009-03-25 13:02:10 +0100 | [diff] [blame] | 422 | else |
| 423 | break; |
| 424 | } else { |
| 425 | if (dohash) |
Bhaskar | 98634f0 | 2013-10-29 23:30:51 -0400 | [diff] [blame] | 426 | start--; |
Benoit | affb481 | 2009-03-25 13:02:10 +0100 | [diff] [blame] | 427 | } |
| 428 | len--; |
| 429 | p--; |
| 430 | } |
Bhaskar | 98634f0 | 2013-10-29 23:30:51 -0400 | [diff] [blame] | 431 | hash = gen_hash(px, start, (end - start)); |
Benoit | affb481 | 2009-03-25 13:02:10 +0100 | [diff] [blame] | 432 | } |
Bhaskar Maddala | b6c0ac9 | 2013-11-05 11:54:02 -0500 | [diff] [blame] | 433 | if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL) |
Willy Tarreau | 798a39c | 2010-11-24 15:04:29 +0100 | [diff] [blame] | 434 | hash = full_hash(hash); |
Willy Tarreau | 39c9ba7 | 2009-10-01 21:11:15 +0200 | [diff] [blame] | 435 | hash_done: |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 436 | if (px->lbprm.algo & BE_LB_LKUP_CHTREE) |
| 437 | return chash_get_server_hash(px, hash); |
| 438 | else |
| 439 | return map_get_server_hash(px, hash); |
Benoit | affb481 | 2009-03-25 13:02:10 +0100 | [diff] [blame] | 440 | } |
| 441 | |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 442 | /* RDP Cookie HASH. */ |
Emeric Brun | 736aa23 | 2009-06-30 17:56:00 +0200 | [diff] [blame] | 443 | struct server *get_server_rch(struct session *s) |
| 444 | { |
| 445 | unsigned long hash = 0; |
| 446 | struct proxy *px = s->be; |
| 447 | unsigned long len; |
Emeric Brun | 736aa23 | 2009-06-30 17:56:00 +0200 | [diff] [blame] | 448 | int ret; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 449 | struct sample smp; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 450 | int rewind; |
Emeric Brun | 736aa23 | 2009-06-30 17:56:00 +0200 | [diff] [blame] | 451 | |
| 452 | /* tot_weight appears to mean srv_count */ |
| 453 | if (px->lbprm.tot_weight == 0) |
| 454 | return NULL; |
| 455 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 456 | memset(&smp, 0, sizeof(smp)); |
Emeric Brun | 736aa23 | 2009-06-30 17:56:00 +0200 | [diff] [blame] | 457 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 458 | b_rew(s->req->buf, rewind = s->req->buf->o); |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 459 | |
Willy Tarreau | cadd8c9 | 2013-07-22 18:09:52 +0200 | [diff] [blame] | 460 | ret = fetch_rdp_cookie_name(s, &smp, px->hh_name, px->hh_len); |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 461 | len = smp.data.str.len; |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 462 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 463 | b_adv(s->req->buf, rewind); |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 464 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 465 | if (ret == 0 || (smp.flags & SMP_F_MAY_CHANGE) || len == 0) |
Emeric Brun | 736aa23 | 2009-06-30 17:56:00 +0200 | [diff] [blame] | 466 | return NULL; |
| 467 | |
Willy Tarreau | 39c9ba7 | 2009-10-01 21:11:15 +0200 | [diff] [blame] | 468 | /* note: we won't hash if there's only one server left */ |
| 469 | if (px->lbprm.tot_used == 1) |
| 470 | goto hash_done; |
| 471 | |
Emeric Brun | 736aa23 | 2009-06-30 17:56:00 +0200 | [diff] [blame] | 472 | /* Found a the hh_name in the headers. |
| 473 | * we will compute the hash based on this value ctx.val. |
| 474 | */ |
Bhaskar | 98634f0 | 2013-10-29 23:30:51 -0400 | [diff] [blame] | 475 | hash = gen_hash(px, smp.data.str.str, len); |
| 476 | |
Bhaskar Maddala | b6c0ac9 | 2013-11-05 11:54:02 -0500 | [diff] [blame] | 477 | if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL) |
Willy Tarreau | 798a39c | 2010-11-24 15:04:29 +0100 | [diff] [blame] | 478 | hash = full_hash(hash); |
Willy Tarreau | 39c9ba7 | 2009-10-01 21:11:15 +0200 | [diff] [blame] | 479 | hash_done: |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 480 | if (px->lbprm.algo & BE_LB_LKUP_CHTREE) |
| 481 | return chash_get_server_hash(px, hash); |
| 482 | else |
| 483 | return map_get_server_hash(px, hash); |
Emeric Brun | 736aa23 | 2009-06-30 17:56:00 +0200 | [diff] [blame] | 484 | } |
Benoit | affb481 | 2009-03-25 13:02:10 +0100 | [diff] [blame] | 485 | |
| 486 | /* |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 487 | * This function applies the load-balancing algorithm to the session, as |
| 488 | * defined by the backend it is assigned to. The session is then marked as |
| 489 | * 'assigned'. |
| 490 | * |
| 491 | * This function MAY NOT be called with SN_ASSIGNED already set. If the session |
| 492 | * had a server previously assigned, it is rebalanced, trying to avoid the same |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 493 | * server, which should still be present in target_srv(&s->target) before the call. |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 494 | * The function tries to keep the original connection slot if it reconnects to |
| 495 | * the same server, otherwise it releases it and tries to offer it. |
| 496 | * |
| 497 | * It is illegal to call this function with a session in a queue. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 498 | * |
| 499 | * It may return : |
Willy Tarreau | 664beb8 | 2011-03-10 11:38:29 +0100 | [diff] [blame] | 500 | * SRV_STATUS_OK if everything is OK. ->srv and ->target are assigned. |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 501 | * SRV_STATUS_NOSRV if no server is available. Session is not ASSIGNED |
| 502 | * SRV_STATUS_FULL if all servers are saturated. Session is not ASSIGNED |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 503 | * SRV_STATUS_INTERNAL for other unrecoverable errors. |
| 504 | * |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 505 | * Upon successful return, the session flag SN_ASSIGNED is set to indicate that |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 506 | * it does not need to be called anymore. This means that target_srv(&s->target) |
| 507 | * can be trusted in balance and direct modes. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 508 | * |
| 509 | */ |
| 510 | |
| 511 | int assign_server(struct session *s) |
| 512 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 513 | struct connection *conn; |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 514 | struct server *conn_slot; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 515 | struct server *srv, *prev_srv; |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 516 | int err; |
Krzysztof Piotr Oledzki | 5a329cf | 2008-02-22 03:50:19 +0100 | [diff] [blame] | 517 | |
Simon Horman | 8effd3d | 2011-08-13 08:03:52 +0900 | [diff] [blame] | 518 | DPRINTF(stderr,"assign_server : s=%p\n",s); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 519 | |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 520 | err = SRV_STATUS_INTERNAL; |
| 521 | if (unlikely(s->pend_pos || s->flags & SN_ASSIGNED)) |
| 522 | goto out_err; |
Krzysztof Piotr Oledzki | 5a329cf | 2008-02-22 03:50:19 +0100 | [diff] [blame] | 523 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 524 | prev_srv = objt_server(s->target); |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 525 | conn_slot = s->srv_conn; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 526 | |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 527 | /* We have to release any connection slot before applying any LB algo, |
| 528 | * otherwise we may erroneously end up with no available slot. |
| 529 | */ |
| 530 | if (conn_slot) |
| 531 | sess_change_server(s, NULL); |
| 532 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 533 | /* We will now try to find the good server and store it into <objt_server(s->target)>. |
| 534 | * Note that <objt_server(s->target)> may be NULL in case of dispatch or proxy mode, |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 535 | * as well as if no server is available (check error code). |
| 536 | */ |
Willy Tarreau | 1a20a5d | 2007-11-01 21:08:19 +0100 | [diff] [blame] | 537 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 538 | srv = NULL; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 539 | s->target = NULL; |
Willy Tarreau | 9420b12 | 2013-12-15 18:58:25 +0100 | [diff] [blame] | 540 | conn = objt_conn(s->req->cons->end); |
Willy Tarreau | 664beb8 | 2011-03-10 11:38:29 +0100 | [diff] [blame] | 541 | |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 542 | if (conn && |
Willy Tarreau | 2481d16 | 2014-02-19 18:40:43 +0100 | [diff] [blame] | 543 | (conn->flags & CO_FL_CONNECTED) && |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 544 | ((s->be->options & PR_O_PREF_LAST) || (s->txn.flags & TX_PREFER_LAST)) && |
Willy Tarreau | 9420b12 | 2013-12-15 18:58:25 +0100 | [diff] [blame] | 545 | objt_server(conn->target) && __objt_server(conn->target)->proxy == s->be && |
| 546 | srv_is_usable(__objt_server(conn->target)->state, __objt_server(conn->target)->eweight)) { |
| 547 | /* This session was relying on a server in a previous request |
| 548 | * and the proxy has "option prefer-current-server" set, so |
| 549 | * let's try to reuse the same server. |
| 550 | */ |
| 551 | srv = __objt_server(conn->target); |
| 552 | s->target = &srv->obj_type; |
| 553 | } |
| 554 | else if (s->be->lbprm.algo & BE_LB_KIND) { |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 555 | /* we must check if we have at least one server available */ |
| 556 | if (!s->be->lbprm.tot_weight) { |
| 557 | err = SRV_STATUS_NOSRV; |
| 558 | goto out; |
| 559 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 560 | |
Willy Tarreau | da76f4f | 2009-10-03 12:36:05 +0200 | [diff] [blame] | 561 | /* First check whether we need to fetch some data or simply call |
| 562 | * the LB lookup function. Only the hashing functions will need |
| 563 | * some input data in fact, and will support multiple algorithms. |
| 564 | */ |
| 565 | switch (s->be->lbprm.algo & BE_LB_LKUP) { |
| 566 | case BE_LB_LKUP_RRTREE: |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 567 | srv = fwrr_get_next_server(s->be, prev_srv); |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 568 | break; |
Willy Tarreau | da76f4f | 2009-10-03 12:36:05 +0200 | [diff] [blame] | 569 | |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 570 | case BE_LB_LKUP_FSTREE: |
| 571 | srv = fas_get_next_server(s->be, prev_srv); |
| 572 | break; |
| 573 | |
Willy Tarreau | da76f4f | 2009-10-03 12:36:05 +0200 | [diff] [blame] | 574 | case BE_LB_LKUP_LCTREE: |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 575 | srv = fwlc_get_next_server(s->be, prev_srv); |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 576 | break; |
Willy Tarreau | da76f4f | 2009-10-03 12:36:05 +0200 | [diff] [blame] | 577 | |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 578 | case BE_LB_LKUP_CHTREE: |
Willy Tarreau | da76f4f | 2009-10-03 12:36:05 +0200 | [diff] [blame] | 579 | case BE_LB_LKUP_MAP: |
Willy Tarreau | 9757a38 | 2009-10-03 12:56:50 +0200 | [diff] [blame] | 580 | if ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR) { |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 581 | if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE) |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 582 | srv = chash_get_next_server(s->be, prev_srv); |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 583 | else |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 584 | srv = map_get_server_rr(s->be, prev_srv); |
Willy Tarreau | 9757a38 | 2009-10-03 12:56:50 +0200 | [diff] [blame] | 585 | break; |
| 586 | } |
| 587 | else if ((s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_HI) { |
Willy Tarreau | da76f4f | 2009-10-03 12:36:05 +0200 | [diff] [blame] | 588 | /* unknown balancing algorithm */ |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 589 | err = SRV_STATUS_INTERNAL; |
| 590 | goto out; |
| 591 | } |
Willy Tarreau | da76f4f | 2009-10-03 12:36:05 +0200 | [diff] [blame] | 592 | |
| 593 | switch (s->be->lbprm.algo & BE_LB_PARM) { |
| 594 | case BE_LB_HASH_SRC: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 595 | conn = objt_conn(s->req->prod->end); |
| 596 | if (conn && conn->addr.from.ss_family == AF_INET) { |
Willy Tarreau | 5dd7fa1 | 2012-03-31 19:53:37 +0200 | [diff] [blame] | 597 | srv = get_server_sh(s->be, |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 598 | (void *)&((struct sockaddr_in *)&conn->addr.from)->sin_addr, |
Willy Tarreau | 5dd7fa1 | 2012-03-31 19:53:37 +0200 | [diff] [blame] | 599 | 4); |
| 600 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 601 | else if (conn && conn->addr.from.ss_family == AF_INET6) { |
Willy Tarreau | 5dd7fa1 | 2012-03-31 19:53:37 +0200 | [diff] [blame] | 602 | srv = get_server_sh(s->be, |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 603 | (void *)&((struct sockaddr_in6 *)&conn->addr.from)->sin6_addr, |
Willy Tarreau | 5dd7fa1 | 2012-03-31 19:53:37 +0200 | [diff] [blame] | 604 | 16); |
| 605 | } |
Willy Tarreau | da76f4f | 2009-10-03 12:36:05 +0200 | [diff] [blame] | 606 | else { |
| 607 | /* unknown IP family */ |
| 608 | err = SRV_STATUS_INTERNAL; |
| 609 | goto out; |
| 610 | } |
Willy Tarreau | da76f4f | 2009-10-03 12:36:05 +0200 | [diff] [blame] | 611 | break; |
| 612 | |
| 613 | case BE_LB_HASH_URI: |
| 614 | /* URI hashing */ |
Willy Tarreau | eaed5a1 | 2010-03-24 14:54:30 +0100 | [diff] [blame] | 615 | if (s->txn.req.msg_state < HTTP_MSG_BODY) |
| 616 | break; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 617 | srv = get_server_uh(s->be, |
Willy Tarreau | da6eed6 | 2014-04-17 20:24:24 +0200 | [diff] [blame^] | 618 | b_ptr(s->req->buf, -http_uri_rewind(&s->txn.req)), |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 619 | s->txn.req.sl.rq.u_l); |
Willy Tarreau | da76f4f | 2009-10-03 12:36:05 +0200 | [diff] [blame] | 620 | break; |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 621 | |
Willy Tarreau | da76f4f | 2009-10-03 12:36:05 +0200 | [diff] [blame] | 622 | case BE_LB_HASH_PRM: |
| 623 | /* URL Parameter hashing */ |
Willy Tarreau | eaed5a1 | 2010-03-24 14:54:30 +0100 | [diff] [blame] | 624 | if (s->txn.req.msg_state < HTTP_MSG_BODY) |
| 625 | break; |
Willy Tarreau | 61a21a3 | 2011-03-01 20:35:49 +0100 | [diff] [blame] | 626 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 627 | srv = get_server_ph(s->be, |
Willy Tarreau | da6eed6 | 2014-04-17 20:24:24 +0200 | [diff] [blame^] | 628 | b_ptr(s->req->buf, -http_uri_rewind(&s->txn.req)), |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 629 | s->txn.req.sl.rq.u_l); |
Willy Tarreau | 61a21a3 | 2011-03-01 20:35:49 +0100 | [diff] [blame] | 630 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 631 | if (!srv && s->txn.meth == HTTP_METH_POST) |
| 632 | srv = get_server_ph_post(s); |
Willy Tarreau | da76f4f | 2009-10-03 12:36:05 +0200 | [diff] [blame] | 633 | break; |
Benoit | affb481 | 2009-03-25 13:02:10 +0100 | [diff] [blame] | 634 | |
Willy Tarreau | da76f4f | 2009-10-03 12:36:05 +0200 | [diff] [blame] | 635 | case BE_LB_HASH_HDR: |
| 636 | /* Header Parameter hashing */ |
Willy Tarreau | eaed5a1 | 2010-03-24 14:54:30 +0100 | [diff] [blame] | 637 | if (s->txn.req.msg_state < HTTP_MSG_BODY) |
| 638 | break; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 639 | srv = get_server_hh(s); |
Willy Tarreau | da76f4f | 2009-10-03 12:36:05 +0200 | [diff] [blame] | 640 | break; |
| 641 | |
| 642 | case BE_LB_HASH_RDP: |
| 643 | /* RDP Cookie hashing */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 644 | srv = get_server_rch(s); |
Willy Tarreau | da76f4f | 2009-10-03 12:36:05 +0200 | [diff] [blame] | 645 | break; |
| 646 | |
| 647 | default: |
| 648 | /* unknown balancing algorithm */ |
| 649 | err = SRV_STATUS_INTERNAL; |
| 650 | goto out; |
Benoit | affb481 | 2009-03-25 13:02:10 +0100 | [diff] [blame] | 651 | } |
Emeric Brun | 736aa23 | 2009-06-30 17:56:00 +0200 | [diff] [blame] | 652 | |
Willy Tarreau | da76f4f | 2009-10-03 12:36:05 +0200 | [diff] [blame] | 653 | /* If the hashing parameter was not found, let's fall |
| 654 | * back to round robin on the map. |
| 655 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 656 | if (!srv) { |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 657 | if (s->be->lbprm.algo & BE_LB_LKUP_CHTREE) |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 658 | srv = chash_get_next_server(s->be, prev_srv); |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 659 | else |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 660 | srv = map_get_server_rr(s->be, prev_srv); |
Willy Tarreau | 6b2e11b | 2009-10-01 07:52:15 +0200 | [diff] [blame] | 661 | } |
Willy Tarreau | da76f4f | 2009-10-03 12:36:05 +0200 | [diff] [blame] | 662 | |
| 663 | /* end of map-based LB */ |
Emeric Brun | 736aa23 | 2009-06-30 17:56:00 +0200 | [diff] [blame] | 664 | break; |
Willy Tarreau | da76f4f | 2009-10-03 12:36:05 +0200 | [diff] [blame] | 665 | |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 666 | default: |
| 667 | /* unknown balancing algorithm */ |
| 668 | err = SRV_STATUS_INTERNAL; |
| 669 | goto out; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 670 | } |
Willy Tarreau | da76f4f | 2009-10-03 12:36:05 +0200 | [diff] [blame] | 671 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 672 | if (!srv) { |
Willy Tarreau | da76f4f | 2009-10-03 12:36:05 +0200 | [diff] [blame] | 673 | err = SRV_STATUS_FULL; |
| 674 | goto out; |
| 675 | } |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 676 | else if (srv != prev_srv) { |
Bhaskar Maddala | a20cb85 | 2014-02-03 16:26:46 -0500 | [diff] [blame] | 677 | be_set_sess_last(s->be); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 678 | s->be->be_counters.cum_lbconn++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 679 | srv->counters.cum_lbconn++; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 680 | } |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 681 | s->target = &srv->obj_type; |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 682 | } |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 683 | else if (s->be->options & (PR_O_DISPATCH | PR_O_TRANSP)) { |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 684 | s->target = &s->be->obj_type; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 685 | } |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 686 | else if ((s->be->options & PR_O_HTTP_PROXY) && |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 687 | (conn = objt_conn(s->req->cons->end)) && |
| 688 | is_addr(&conn->addr.to)) { |
Willy Tarreau | 664beb8 | 2011-03-10 11:38:29 +0100 | [diff] [blame] | 689 | /* in proxy mode, we need a valid destination address */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 690 | s->target = &s->be->obj_type; |
Willy Tarreau | 664beb8 | 2011-03-10 11:38:29 +0100 | [diff] [blame] | 691 | } |
| 692 | else { |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 693 | err = SRV_STATUS_NOSRV; |
| 694 | goto out; |
| 695 | } |
| 696 | |
| 697 | s->flags |= SN_ASSIGNED; |
| 698 | err = SRV_STATUS_OK; |
| 699 | out: |
| 700 | |
| 701 | /* Either we take back our connection slot, or we offer it to someone |
| 702 | * else if we don't need it anymore. |
| 703 | */ |
| 704 | if (conn_slot) { |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 705 | if (conn_slot == srv) { |
| 706 | sess_change_server(s, srv); |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 707 | } else { |
| 708 | if (may_dequeue_tasks(conn_slot, s->be)) |
| 709 | process_srv_queue(conn_slot); |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | out_err: |
| 714 | return err; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | |
| 718 | /* |
| 719 | * This function assigns a server address to a session, and sets SN_ADDR_SET. |
| 720 | * The address is taken from the currently assigned server, or from the |
| 721 | * dispatch or transparent address. |
| 722 | * |
| 723 | * It may return : |
| 724 | * SRV_STATUS_OK if everything is OK. |
| 725 | * SRV_STATUS_INTERNAL for other unrecoverable errors. |
| 726 | * |
| 727 | * Upon successful return, the session flag SN_ADDR_SET is set. This flag is |
| 728 | * not cleared, so it's to the caller to clear it if required. |
| 729 | * |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 730 | * The caller is responsible for having already assigned a connection |
| 731 | * to si->end. |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 732 | * |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 733 | */ |
| 734 | int assign_server_address(struct session *s) |
| 735 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 736 | struct connection *cli_conn = objt_conn(s->req->prod->end); |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 737 | struct connection *srv_conn = objt_conn(s->req->cons->end); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 738 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 739 | #ifdef DEBUG_FULL |
| 740 | fprintf(stderr,"assign_server_address : s=%p\n",s); |
| 741 | #endif |
| 742 | |
Willy Tarreau | f3e49f9 | 2009-10-03 12:21:20 +0200 | [diff] [blame] | 743 | if ((s->flags & SN_DIRECT) || (s->be->lbprm.algo & BE_LB_KIND)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 744 | /* A server is necessarily known for this session */ |
| 745 | if (!(s->flags & SN_ASSIGNED)) |
| 746 | return SRV_STATUS_INTERNAL; |
| 747 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 748 | srv_conn->addr.to = objt_server(s->target)->addr; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 749 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 750 | if (!is_addr(&srv_conn->addr.to) && cli_conn) { |
Willy Tarreau | d669a4f | 2010-07-13 14:49:50 +0200 | [diff] [blame] | 751 | /* if the server has no address, we use the same address |
| 752 | * the client asked, which is handy for remapping ports |
| 753 | * locally on multiple addresses at once. |
| 754 | */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 755 | conn_get_to_addr(cli_conn); |
Willy Tarreau | d669a4f | 2010-07-13 14:49:50 +0200 | [diff] [blame] | 756 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 757 | if (cli_conn->addr.to.ss_family == AF_INET) { |
| 758 | ((struct sockaddr_in *)&srv_conn->addr.to)->sin_addr = ((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr; |
| 759 | } else if (cli_conn->addr.to.ss_family == AF_INET6) { |
| 760 | ((struct sockaddr_in6 *)&srv_conn->addr.to)->sin6_addr = ((struct sockaddr_in6 *)&cli_conn->addr.to)->sin6_addr; |
Emeric Brun | ec810d1 | 2010-10-22 16:36:33 +0200 | [diff] [blame] | 761 | } |
Willy Tarreau | d669a4f | 2010-07-13 14:49:50 +0200 | [diff] [blame] | 762 | } |
| 763 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 764 | /* if this server remaps proxied ports, we'll use |
| 765 | * the port the client connected to with an offset. */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 766 | if ((objt_server(s->target)->state & SRV_MAPPORTS) && cli_conn) { |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 767 | int base_port; |
| 768 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 769 | conn_get_to_addr(cli_conn); |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 770 | |
| 771 | /* First, retrieve the port from the incoming connection */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 772 | base_port = get_host_port(&cli_conn->addr.to); |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 773 | |
| 774 | /* Second, assign the outgoing connection's port */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 775 | base_port += get_host_port(&srv_conn->addr.to); |
| 776 | set_host_port(&srv_conn->addr.to, base_port); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 777 | } |
| 778 | } |
Willy Tarreau | 1620ec3 | 2011-08-06 17:05:02 +0200 | [diff] [blame] | 779 | else if (s->be->options & PR_O_DISPATCH) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 780 | /* connect to the defined dispatch addr */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 781 | srv_conn->addr.to = s->be->dispatch_addr; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 782 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 783 | else if ((s->be->options & PR_O_TRANSP) && cli_conn) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 784 | /* in transparent mode, use the original dest addr if no dispatch specified */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 785 | conn_get_to_addr(cli_conn); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 786 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 787 | if (cli_conn->addr.to.ss_family == AF_INET || cli_conn->addr.to.ss_family == AF_INET6) |
| 788 | srv_conn->addr.to = cli_conn->addr.to; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 789 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 790 | else if (s->be->options & PR_O_HTTP_PROXY) { |
| 791 | /* If HTTP PROXY option is set, then server is already assigned |
| 792 | * during incoming client request parsing. */ |
| 793 | } |
Willy Tarreau | 1a1158b | 2007-01-20 11:07:46 +0100 | [diff] [blame] | 794 | else { |
| 795 | /* no server and no LB algorithm ! */ |
| 796 | return SRV_STATUS_INTERNAL; |
| 797 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 798 | |
| 799 | s->flags |= SN_ADDR_SET; |
| 800 | return SRV_STATUS_OK; |
| 801 | } |
| 802 | |
| 803 | |
| 804 | /* This function assigns a server to session <s> if required, and can add the |
| 805 | * connection to either the assigned server's queue or to the proxy's queue. |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 806 | * If ->srv_conn is set, the session is first released from the server. |
| 807 | * It may also be called with SN_DIRECT and/or SN_ASSIGNED though. It will |
| 808 | * be called before any connection and after any retry or redispatch occurs. |
| 809 | * |
| 810 | * It is not allowed to call this function with a session in a queue. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 811 | * |
| 812 | * Returns : |
| 813 | * |
| 814 | * SRV_STATUS_OK if everything is OK. |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 815 | * SRV_STATUS_NOSRV if no server is available. objt_server(s->target) = NULL. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 816 | * SRV_STATUS_QUEUED if the connection has been queued. |
| 817 | * SRV_STATUS_FULL if the server(s) is/are saturated and the |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 818 | * connection could not be queued at the server's, |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 819 | * which may be NULL if we queue on the backend. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 820 | * SRV_STATUS_INTERNAL for other unrecoverable errors. |
| 821 | * |
| 822 | */ |
| 823 | int assign_server_and_queue(struct session *s) |
| 824 | { |
| 825 | struct pendconn *p; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 826 | struct server *srv; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 827 | int err; |
| 828 | |
| 829 | if (s->pend_pos) |
| 830 | return SRV_STATUS_INTERNAL; |
| 831 | |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 832 | err = SRV_STATUS_OK; |
| 833 | if (!(s->flags & SN_ASSIGNED)) { |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 834 | struct server *prev_srv = objt_server(s->target); |
Willy Tarreau | 3d80d91 | 2011-03-10 11:42:13 +0100 | [diff] [blame] | 835 | |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 836 | err = assign_server(s); |
Willy Tarreau | 3d80d91 | 2011-03-10 11:42:13 +0100 | [diff] [blame] | 837 | if (prev_srv) { |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 838 | /* This session was previously assigned to a server. We have to |
| 839 | * update the session's and the server's stats : |
| 840 | * - if the server changed : |
| 841 | * - set TX_CK_DOWN if txn.flags was TX_CK_VALID |
| 842 | * - set SN_REDISP if it was successfully redispatched |
| 843 | * - increment srv->redispatches and be->redispatches |
| 844 | * - if the server remained the same : update retries. |
Krzysztof Piotr Oledzki | 5a329cf | 2008-02-22 03:50:19 +0100 | [diff] [blame] | 845 | */ |
| 846 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 847 | if (prev_srv != objt_server(s->target)) { |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 848 | if ((s->txn.flags & TX_CK_MASK) == TX_CK_VALID) { |
| 849 | s->txn.flags &= ~TX_CK_MASK; |
| 850 | s->txn.flags |= TX_CK_DOWN; |
| 851 | } |
| 852 | s->flags |= SN_REDISP; |
Willy Tarreau | 3d80d91 | 2011-03-10 11:42:13 +0100 | [diff] [blame] | 853 | prev_srv->counters.redispatches++; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 854 | s->be->be_counters.redispatches++; |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 855 | } else { |
Willy Tarreau | 3d80d91 | 2011-03-10 11:42:13 +0100 | [diff] [blame] | 856 | prev_srv->counters.retries++; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 857 | s->be->be_counters.retries++; |
Krzysztof Piotr Oledzki | 5a329cf | 2008-02-22 03:50:19 +0100 | [diff] [blame] | 858 | } |
Krzysztof Piotr Oledzki | 5a329cf | 2008-02-22 03:50:19 +0100 | [diff] [blame] | 859 | } |
| 860 | } |
| 861 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 862 | switch (err) { |
| 863 | case SRV_STATUS_OK: |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 864 | /* we have SN_ASSIGNED set */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 865 | srv = objt_server(s->target); |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 866 | if (!srv) |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 867 | return SRV_STATUS_OK; /* dispatch or proxy mode */ |
| 868 | |
| 869 | /* If we already have a connection slot, no need to check any queue */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 870 | if (s->srv_conn == srv) |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 871 | return SRV_STATUS_OK; |
| 872 | |
| 873 | /* OK, this session already has an assigned server, but no |
| 874 | * connection slot yet. Either it is a redispatch, or it was |
| 875 | * assigned from persistence information (direct mode). |
| 876 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 877 | if ((s->flags & SN_REDIRECTABLE) && srv->rdr_len) { |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 878 | /* server scheduled for redirection, and already assigned. We |
| 879 | * don't want to go further nor check the queue. |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 880 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 881 | sess_change_server(s, srv); /* not really needed in fact */ |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 882 | return SRV_STATUS_OK; |
| 883 | } |
| 884 | |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 885 | /* We might have to queue this session if the assigned server is full. |
| 886 | * We know we have to queue it into the server's queue, so if a maxqueue |
| 887 | * is set on the server, we must also check that the server's queue is |
| 888 | * not full, in which case we have to return FULL. |
| 889 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 890 | if (srv->maxconn && |
| 891 | (srv->nbpend || srv->served >= srv_dynamic_maxconn(srv))) { |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 892 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 893 | if (srv->maxqueue > 0 && srv->nbpend >= srv->maxqueue) |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 894 | return SRV_STATUS_FULL; |
| 895 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 896 | p = pendconn_add(s); |
| 897 | if (p) |
| 898 | return SRV_STATUS_QUEUED; |
| 899 | else |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 900 | return SRV_STATUS_INTERNAL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 901 | } |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 902 | |
| 903 | /* OK, we can use this server. Let's reserve our place */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 904 | sess_change_server(s, srv); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 905 | return SRV_STATUS_OK; |
| 906 | |
| 907 | case SRV_STATUS_FULL: |
| 908 | /* queue this session into the proxy's queue */ |
| 909 | p = pendconn_add(s); |
| 910 | if (p) |
| 911 | return SRV_STATUS_QUEUED; |
| 912 | else |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 913 | return SRV_STATUS_INTERNAL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 914 | |
| 915 | case SRV_STATUS_NOSRV: |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 916 | return err; |
| 917 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 918 | case SRV_STATUS_INTERNAL: |
| 919 | return err; |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 920 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 921 | default: |
| 922 | return SRV_STATUS_INTERNAL; |
| 923 | } |
Willy Tarreau | 5b6995c | 2008-01-13 16:31:17 +0100 | [diff] [blame] | 924 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 925 | |
Willy Tarreau | b1d6774 | 2010-03-29 19:36:59 +0200 | [diff] [blame] | 926 | /* If an explicit source binding is specified on the server and/or backend, and |
| 927 | * this source makes use of the transparent proxy, then it is extracted now and |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 928 | * assigned to the session's pending connection. This function assumes that an |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 929 | * outgoing connection has already been assigned to s->req->cons->end. |
Willy Tarreau | b1d6774 | 2010-03-29 19:36:59 +0200 | [diff] [blame] | 930 | */ |
| 931 | static void assign_tproxy_address(struct session *s) |
| 932 | { |
Pieter Baauw | d551fb5 | 2013-05-08 22:49:23 +0200 | [diff] [blame] | 933 | #if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_TRANSPARENT) |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 934 | struct server *srv = objt_server(s->target); |
Willy Tarreau | 9cd7d6c | 2012-12-08 23:13:33 +0100 | [diff] [blame] | 935 | struct conn_src *src; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 936 | struct connection *cli_conn; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 937 | struct connection *srv_conn = objt_conn(s->req->cons->end); |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 938 | |
Willy Tarreau | 9cd7d6c | 2012-12-08 23:13:33 +0100 | [diff] [blame] | 939 | if (srv && srv->conn_src.opts & CO_SRC_BIND) |
| 940 | src = &srv->conn_src; |
| 941 | else if (s->be->conn_src.opts & CO_SRC_BIND) |
| 942 | src = &s->be->conn_src; |
| 943 | else |
| 944 | return; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 945 | |
Willy Tarreau | 9cd7d6c | 2012-12-08 23:13:33 +0100 | [diff] [blame] | 946 | switch (src->opts & CO_SRC_TPROXY_MASK) { |
| 947 | case CO_SRC_TPROXY_ADDR: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 948 | srv_conn->addr.from = src->tproxy_addr; |
Willy Tarreau | 9cd7d6c | 2012-12-08 23:13:33 +0100 | [diff] [blame] | 949 | break; |
| 950 | case CO_SRC_TPROXY_CLI: |
| 951 | case CO_SRC_TPROXY_CIP: |
| 952 | /* FIXME: what can we do if the client connects in IPv6 or unix socket ? */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 953 | cli_conn = objt_conn(s->req->prod->end); |
| 954 | if (cli_conn) |
| 955 | srv_conn->addr.from = cli_conn->addr.from; |
| 956 | else |
| 957 | memset(&srv_conn->addr.from, 0, sizeof(srv_conn->addr.from)); |
Willy Tarreau | 9cd7d6c | 2012-12-08 23:13:33 +0100 | [diff] [blame] | 958 | break; |
| 959 | case CO_SRC_TPROXY_DYN: |
| 960 | if (src->bind_hdr_occ) { |
| 961 | char *vptr; |
| 962 | int vlen; |
| 963 | int rewind; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 964 | |
Willy Tarreau | 9cd7d6c | 2012-12-08 23:13:33 +0100 | [diff] [blame] | 965 | /* bind to the IP in a header */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 966 | ((struct sockaddr_in *)&srv_conn->addr.from)->sin_family = AF_INET; |
| 967 | ((struct sockaddr_in *)&srv_conn->addr.from)->sin_port = 0; |
| 968 | ((struct sockaddr_in *)&srv_conn->addr.from)->sin_addr.s_addr = 0; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 969 | |
Willy Tarreau | 211cdec | 2014-04-17 20:18:08 +0200 | [diff] [blame] | 970 | b_rew(s->req->buf, rewind = http_hdr_rewind(&s->txn.req)); |
Willy Tarreau | 9cd7d6c | 2012-12-08 23:13:33 +0100 | [diff] [blame] | 971 | if (http_get_hdr(&s->txn.req, src->bind_hdr_name, src->bind_hdr_len, |
| 972 | &s->txn.hdr_idx, src->bind_hdr_occ, NULL, &vptr, &vlen)) { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 973 | ((struct sockaddr_in *)&srv_conn->addr.from)->sin_addr.s_addr = |
Willy Tarreau | 9cd7d6c | 2012-12-08 23:13:33 +0100 | [diff] [blame] | 974 | htonl(inetaddr_host_lim(vptr, vptr + vlen)); |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 975 | } |
Willy Tarreau | 9cd7d6c | 2012-12-08 23:13:33 +0100 | [diff] [blame] | 976 | b_adv(s->req->buf, rewind); |
Willy Tarreau | b1d6774 | 2010-03-29 19:36:59 +0200 | [diff] [blame] | 977 | } |
Willy Tarreau | 9cd7d6c | 2012-12-08 23:13:33 +0100 | [diff] [blame] | 978 | break; |
| 979 | default: |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 980 | memset(&srv_conn->addr.from, 0, sizeof(srv_conn->addr.from)); |
Willy Tarreau | b1d6774 | 2010-03-29 19:36:59 +0200 | [diff] [blame] | 981 | } |
| 982 | #endif |
| 983 | } |
| 984 | |
| 985 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 986 | /* |
| 987 | * This function initiates a connection to the server assigned to this session |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 988 | * (s->target, s->req->cons->addr.to). It will assign a server if none |
Willy Tarreau | 664beb8 | 2011-03-10 11:38:29 +0100 | [diff] [blame] | 989 | * is assigned yet. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 990 | * It can return one of : |
| 991 | * - SN_ERR_NONE if everything's OK |
| 992 | * - SN_ERR_SRVTO if there are no more servers |
| 993 | * - SN_ERR_SRVCL if the connection was refused by the server |
| 994 | * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn) |
| 995 | * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...) |
| 996 | * - SN_ERR_INTERNAL for any other purely internal errors |
| 997 | * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted. |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 998 | * The server-facing stream interface is expected to hold a pre-allocated connection |
| 999 | * in s->req->cons->conn. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1000 | */ |
| 1001 | int connect_server(struct session *s) |
| 1002 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1003 | struct connection *cli_conn; |
Willy Tarreau | 34601a8 | 2013-12-15 16:33:46 +0100 | [diff] [blame] | 1004 | struct connection *srv_conn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1005 | struct server *srv; |
Willy Tarreau | 34601a8 | 2013-12-15 16:33:46 +0100 | [diff] [blame] | 1006 | int reuse = 0; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 1007 | int err; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1008 | |
Willy Tarreau | 34601a8 | 2013-12-15 16:33:46 +0100 | [diff] [blame] | 1009 | srv_conn = objt_conn(s->req->cons->end); |
| 1010 | if (srv_conn) |
| 1011 | reuse = s->target == srv_conn->target; |
| 1012 | |
| 1013 | if (reuse) { |
| 1014 | /* Disable connection reuse if a dynamic source is used. |
| 1015 | * As long as we don't share connections between servers, |
| 1016 | * we don't need to disable connection reuse on no-idempotent |
| 1017 | * requests nor when PROXY protocol is used. |
| 1018 | */ |
| 1019 | srv = objt_server(s->target); |
| 1020 | if (srv && srv->conn_src.opts & CO_SRC_BIND) { |
| 1021 | if ((srv->conn_src.opts & CO_SRC_TPROXY_MASK) == CO_SRC_TPROXY_DYN) |
| 1022 | reuse = 0; |
| 1023 | } |
| 1024 | else if (s->be->conn_src.opts & CO_SRC_BIND) { |
| 1025 | if ((s->be->conn_src.opts & CO_SRC_TPROXY_MASK) == CO_SRC_TPROXY_DYN) |
| 1026 | reuse = 0; |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | srv_conn = si_alloc_conn(s->req->cons, reuse); |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 1031 | if (!srv_conn) |
| 1032 | return SN_ERR_RESOURCE; |
| 1033 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1034 | if (!(s->flags & SN_ADDR_SET)) { |
| 1035 | err = assign_server_address(s); |
| 1036 | if (err != SRV_STATUS_OK) |
| 1037 | return SN_ERR_INTERNAL; |
| 1038 | } |
| 1039 | |
Willy Tarreau | ff605db | 2013-12-20 11:09:51 +0100 | [diff] [blame] | 1040 | if (!conn_xprt_ready(srv_conn)) { |
| 1041 | /* the target was only on the session, assign it to the SI now */ |
| 1042 | srv_conn->target = s->target; |
Willy Tarreau | d88edf2 | 2009-06-14 15:48:17 +0200 | [diff] [blame] | 1043 | |
Willy Tarreau | ff605db | 2013-12-20 11:09:51 +0100 | [diff] [blame] | 1044 | /* set the correct protocol on the output stream interface */ |
| 1045 | if (objt_server(s->target)) { |
| 1046 | conn_prepare(srv_conn, objt_server(s->target)->proto, objt_server(s->target)->xprt); |
| 1047 | } |
| 1048 | else if (obj_type(s->target) == OBJ_TYPE_PROXY) { |
| 1049 | /* proxies exclusively run on raw_sock right now */ |
| 1050 | conn_prepare(srv_conn, protocol_by_family(srv_conn->addr.to.ss_family), &raw_sock); |
| 1051 | if (!objt_conn(s->req->cons->end) || !objt_conn(s->req->cons->end)->ctrl) |
| 1052 | return SN_ERR_INTERNAL; |
| 1053 | } |
| 1054 | else |
| 1055 | return SN_ERR_INTERNAL; /* how did we get there ? */ |
Willy Tarreau | d02394b | 2012-05-11 18:32:18 +0200 | [diff] [blame] | 1056 | |
Willy Tarreau | ff605db | 2013-12-20 11:09:51 +0100 | [diff] [blame] | 1057 | /* process the case where the server requires the PROXY protocol to be sent */ |
| 1058 | srv_conn->send_proxy_ofs = 0; |
| 1059 | if (objt_server(s->target) && (objt_server(s->target)->state & SRV_SEND_PROXY)) { |
| 1060 | srv_conn->send_proxy_ofs = 1; /* must compute size */ |
| 1061 | cli_conn = objt_conn(s->req->prod->end); |
| 1062 | if (cli_conn) |
| 1063 | conn_get_to_addr(cli_conn); |
| 1064 | } |
Willy Tarreau | 2a6e880 | 2013-10-24 15:50:53 +0200 | [diff] [blame] | 1065 | |
Willy Tarreau | ff605db | 2013-12-20 11:09:51 +0100 | [diff] [blame] | 1066 | si_attach_conn(s->req->cons, srv_conn); |
Willy Tarreau | 26d8c59 | 2012-05-07 18:12:14 +0200 | [diff] [blame] | 1067 | |
Willy Tarreau | ff605db | 2013-12-20 11:09:51 +0100 | [diff] [blame] | 1068 | assign_tproxy_address(s); |
| 1069 | } |
| 1070 | else { |
| 1071 | /* the connection is being reused, just re-attach it */ |
| 1072 | si_attach_conn(s->req->cons, srv_conn); |
Willy Tarreau | 3634624 | 2014-02-24 18:26:30 +0100 | [diff] [blame] | 1073 | s->flags |= SN_SRV_REUSED; |
Willy Tarreau | ff605db | 2013-12-20 11:09:51 +0100 | [diff] [blame] | 1074 | } |
Willy Tarreau | b1d6774 | 2010-03-29 19:36:59 +0200 | [diff] [blame] | 1075 | |
William Lallemand | b7ff6a3 | 2012-03-02 14:35:21 +0100 | [diff] [blame] | 1076 | /* flag for logging source ip/port */ |
| 1077 | if (s->fe->options2 & PR_O2_SRC_ADDR) |
| 1078 | s->req->cons->flags |= SI_FL_SRC_ADDR; |
| 1079 | |
Willy Tarreau | 14f8e86 | 2012-08-30 22:23:13 +0200 | [diff] [blame] | 1080 | /* disable lingering */ |
| 1081 | if (s->be->options & PR_O_TCP_NOLING) |
| 1082 | s->req->cons->flags |= SI_FL_NOLINGER; |
| 1083 | |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 1084 | err = si_connect(s->req->cons); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1085 | |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 1086 | if (err != SN_ERR_NONE) |
| 1087 | return err; |
Willy Tarreau | 788e284 | 2008-08-26 13:25:39 +0200 | [diff] [blame] | 1088 | |
Willy Tarreau | 14f8e86 | 2012-08-30 22:23:13 +0200 | [diff] [blame] | 1089 | /* set connect timeout */ |
| 1090 | s->req->cons->exp = tick_add_ifset(now_ms, s->be->timeout.connect); |
| 1091 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 1092 | srv = objt_server(s->target); |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1093 | if (srv) { |
Willy Tarreau | 1e62de6 | 2008-11-11 20:20:02 +0100 | [diff] [blame] | 1094 | s->flags |= SN_CURR_SESS; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1095 | srv->cur_sess++; |
| 1096 | if (srv->cur_sess > srv->counters.cur_sess_max) |
| 1097 | srv->counters.cur_sess_max = srv->cur_sess; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 1098 | if (s->be->lbprm.server_take_conn) |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1099 | s->be->lbprm.server_take_conn(srv); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1100 | } |
| 1101 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1102 | return SN_ERR_NONE; /* connection is OK */ |
| 1103 | } |
| 1104 | |
| 1105 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1106 | /* This function performs the "redispatch" part of a connection attempt. It |
| 1107 | * will assign a server if required, queue the connection if required, and |
| 1108 | * handle errors that might arise at this level. It can change the server |
| 1109 | * state. It will return 1 if it encounters an error, switches the server |
| 1110 | * state, or has to queue a connection. Otherwise, it will return 0 indicating |
| 1111 | * that the connection is ready to use. |
| 1112 | */ |
| 1113 | |
| 1114 | int srv_redispatch_connect(struct session *t) |
| 1115 | { |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1116 | struct server *srv; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1117 | int conn_err; |
| 1118 | |
| 1119 | /* We know that we don't have any connection pending, so we will |
| 1120 | * try to get a new one, and wait in this state if it's queued |
| 1121 | */ |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1122 | redispatch: |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1123 | conn_err = assign_server_and_queue(t); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 1124 | srv = objt_server(t->target); |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1125 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1126 | switch (conn_err) { |
| 1127 | case SRV_STATUS_OK: |
| 1128 | break; |
| 1129 | |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1130 | case SRV_STATUS_FULL: |
| 1131 | /* The server has reached its maxqueue limit. Either PR_O_REDISP is set |
| 1132 | * and we can redispatch to another server, or it is not and we return |
| 1133 | * 503. This only makes sense in DIRECT mode however, because normal LB |
| 1134 | * algorithms would never select such a server, and hash algorithms |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1135 | * would bring us on the same server again. Note that t->target is set |
| 1136 | * in this case. |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1137 | */ |
Willy Tarreau | 4de9149 | 2010-01-22 19:10:05 +0100 | [diff] [blame] | 1138 | if (((t->flags & (SN_DIRECT|SN_FORCE_PRST)) == SN_DIRECT) && |
| 1139 | (t->be->options & PR_O_REDISP)) { |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1140 | t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET); |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1141 | goto redispatch; |
| 1142 | } |
| 1143 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 1144 | if (!t->req->cons->err_type) { |
| 1145 | t->req->cons->err_type = SI_ET_QUEUE_ERR; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 1146 | } |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1147 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1148 | srv->counters.failed_conns++; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 1149 | t->be->be_counters.failed_conns++; |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1150 | return 1; |
| 1151 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1152 | case SRV_STATUS_NOSRV: |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1153 | /* note: it is guaranteed that srv == NULL here */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 1154 | if (!t->req->cons->err_type) { |
| 1155 | t->req->cons->err_type = SI_ET_CONN_ERR; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 1156 | } |
Krzysztof Piotr Oledzki | 5a329cf | 2008-02-22 03:50:19 +0100 | [diff] [blame] | 1157 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 1158 | t->be->be_counters.failed_conns++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1159 | return 1; |
| 1160 | |
| 1161 | case SRV_STATUS_QUEUED: |
Willy Tarreau | 3537467 | 2008-09-03 18:11:02 +0200 | [diff] [blame] | 1162 | t->req->cons->exp = tick_add_ifset(now_ms, t->be->timeout.queue); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 1163 | t->req->cons->state = SI_ST_QUE; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1164 | /* do nothing else and do not wake any other session up */ |
| 1165 | return 1; |
| 1166 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1167 | case SRV_STATUS_INTERNAL: |
| 1168 | default: |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 1169 | if (!t->req->cons->err_type) { |
| 1170 | t->req->cons->err_type = SI_ET_CONN_OTHER; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 1171 | } |
| 1172 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1173 | if (srv) |
| 1174 | srv_inc_sess_ctr(srv); |
| 1175 | if (srv) |
Bhaskar Maddala | a20cb85 | 2014-02-03 16:26:46 -0500 | [diff] [blame] | 1176 | srv_set_sess_last(srv); |
| 1177 | if (srv) |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1178 | srv->counters.failed_conns++; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 1179 | t->be->be_counters.failed_conns++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1180 | |
| 1181 | /* release other sessions waiting for this server */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1182 | if (may_dequeue_tasks(srv, t->be)) |
| 1183 | process_srv_queue(srv); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1184 | return 1; |
| 1185 | } |
| 1186 | /* if we get here, it's because we got SRV_STATUS_OK, which also |
| 1187 | * means that the connection has not been queued. |
| 1188 | */ |
| 1189 | return 0; |
| 1190 | } |
| 1191 | |
Willy Tarreau | 44b90cc | 2010-05-24 20:27:29 +0200 | [diff] [blame] | 1192 | /* Apply RDP cookie persistence to the current session. For this, the function |
| 1193 | * tries to extract an RDP cookie from the request buffer, and look for the |
| 1194 | * matching server in the list. If the server is found, it is assigned to the |
| 1195 | * session. This always returns 1, and the analyser removes itself from the |
| 1196 | * list. Nothing is performed if a server was already assigned. |
| 1197 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 1198 | int tcp_persist_rdp_cookie(struct session *s, struct channel *req, int an_bit) |
Willy Tarreau | 44b90cc | 2010-05-24 20:27:29 +0200 | [diff] [blame] | 1199 | { |
| 1200 | struct proxy *px = s->be; |
| 1201 | int ret; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 1202 | struct sample smp; |
Willy Tarreau | 44b90cc | 2010-05-24 20:27:29 +0200 | [diff] [blame] | 1203 | struct server *srv = px->srv; |
| 1204 | struct sockaddr_in addr; |
| 1205 | char *p; |
| 1206 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 1207 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n", |
Willy Tarreau | 44b90cc | 2010-05-24 20:27:29 +0200 | [diff] [blame] | 1208 | now_ms, __FUNCTION__, |
| 1209 | s, |
| 1210 | req, |
| 1211 | req->rex, req->wex, |
| 1212 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1213 | req->buf->i, |
Willy Tarreau | 44b90cc | 2010-05-24 20:27:29 +0200 | [diff] [blame] | 1214 | req->analysers); |
| 1215 | |
| 1216 | if (s->flags & SN_ASSIGNED) |
| 1217 | goto no_cookie; |
| 1218 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 1219 | memset(&smp, 0, sizeof(smp)); |
Willy Tarreau | 44b90cc | 2010-05-24 20:27:29 +0200 | [diff] [blame] | 1220 | |
Willy Tarreau | cadd8c9 | 2013-07-22 18:09:52 +0200 | [diff] [blame] | 1221 | ret = fetch_rdp_cookie_name(s, &smp, s->be->rdp_cookie_name, s->be->rdp_cookie_len); |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 1222 | if (ret == 0 || (smp.flags & SMP_F_MAY_CHANGE) || smp.data.str.len == 0) |
Willy Tarreau | 44b90cc | 2010-05-24 20:27:29 +0200 | [diff] [blame] | 1223 | goto no_cookie; |
| 1224 | |
| 1225 | memset(&addr, 0, sizeof(addr)); |
| 1226 | addr.sin_family = AF_INET; |
| 1227 | |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 1228 | /* Considering an rdp cookie detected using acl, str ended with <cr><lf> and should return */ |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 1229 | addr.sin_addr.s_addr = strtoul(smp.data.str.str, &p, 10); |
Willy Tarreau | 44b90cc | 2010-05-24 20:27:29 +0200 | [diff] [blame] | 1230 | if (*p != '.') |
| 1231 | goto no_cookie; |
| 1232 | p++; |
| 1233 | addr.sin_port = (unsigned short)strtoul(p, &p, 10); |
| 1234 | if (*p != '.') |
| 1235 | goto no_cookie; |
| 1236 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 1237 | s->target = NULL; |
Willy Tarreau | 44b90cc | 2010-05-24 20:27:29 +0200 | [diff] [blame] | 1238 | while (srv) { |
| 1239 | if (memcmp(&addr, &(srv->addr), sizeof(addr)) == 0) { |
| 1240 | if ((srv->state & SRV_RUNNING) || (px->options & PR_O_PERSIST)) { |
| 1241 | /* we found the server and it is usable */ |
| 1242 | s->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 1243 | s->target = &srv->obj_type; |
Willy Tarreau | 44b90cc | 2010-05-24 20:27:29 +0200 | [diff] [blame] | 1244 | break; |
| 1245 | } |
| 1246 | } |
| 1247 | srv = srv->next; |
| 1248 | } |
| 1249 | |
| 1250 | no_cookie: |
| 1251 | req->analysers &= ~an_bit; |
| 1252 | req->analyse_exp = TICK_ETERNITY; |
| 1253 | return 1; |
| 1254 | } |
| 1255 | |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 1256 | int be_downtime(struct proxy *px) { |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 1257 | if (px->lbprm.tot_weight && px->last_change < now.tv_sec) // ignore negative time |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 1258 | return px->down_time; |
| 1259 | |
| 1260 | return now.tv_sec - px->last_change + px->down_time; |
| 1261 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1262 | |
Krzysztof Piotr Oledzki | 15514c2 | 2010-01-04 16:03:09 +0100 | [diff] [blame] | 1263 | /* |
| 1264 | * This function returns a string containing the balancing |
| 1265 | * mode of the proxy in a format suitable for stats. |
| 1266 | */ |
| 1267 | |
| 1268 | const char *backend_lb_algo_str(int algo) { |
| 1269 | |
| 1270 | if (algo == BE_LB_ALGO_RR) |
| 1271 | return "roundrobin"; |
| 1272 | else if (algo == BE_LB_ALGO_SRR) |
| 1273 | return "static-rr"; |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 1274 | else if (algo == BE_LB_ALGO_FAS) |
| 1275 | return "first"; |
Krzysztof Piotr Oledzki | 15514c2 | 2010-01-04 16:03:09 +0100 | [diff] [blame] | 1276 | else if (algo == BE_LB_ALGO_LC) |
| 1277 | return "leastconn"; |
| 1278 | else if (algo == BE_LB_ALGO_SH) |
| 1279 | return "source"; |
| 1280 | else if (algo == BE_LB_ALGO_UH) |
| 1281 | return "uri"; |
| 1282 | else if (algo == BE_LB_ALGO_PH) |
| 1283 | return "url_param"; |
| 1284 | else if (algo == BE_LB_ALGO_HH) |
| 1285 | return "hdr"; |
| 1286 | else if (algo == BE_LB_ALGO_RCH) |
| 1287 | return "rdp-cookie"; |
| 1288 | else |
| 1289 | return NULL; |
| 1290 | } |
| 1291 | |
Willy Tarreau | a0cbda6 | 2007-11-01 21:39:54 +0100 | [diff] [blame] | 1292 | /* This function parses a "balance" statement in a backend section describing |
| 1293 | * <curproxy>. It returns -1 if there is any error, otherwise zero. If it |
Willy Tarreau | a93c74b | 2012-05-08 18:14:39 +0200 | [diff] [blame] | 1294 | * returns -1, it will write an error message into the <err> buffer which will |
| 1295 | * automatically be allocated and must be passed as NULL. The trailing '\n' |
| 1296 | * will not be written. The function must be called with <args> pointing to the |
| 1297 | * first word after "balance". |
Willy Tarreau | a0cbda6 | 2007-11-01 21:39:54 +0100 | [diff] [blame] | 1298 | */ |
Willy Tarreau | a93c74b | 2012-05-08 18:14:39 +0200 | [diff] [blame] | 1299 | int backend_parse_balance(const char **args, char **err, struct proxy *curproxy) |
Willy Tarreau | a0cbda6 | 2007-11-01 21:39:54 +0100 | [diff] [blame] | 1300 | { |
| 1301 | if (!*(args[0])) { |
| 1302 | /* if no option is set, use round-robin by default */ |
Willy Tarreau | 3168223 | 2007-11-29 15:38:04 +0100 | [diff] [blame] | 1303 | curproxy->lbprm.algo &= ~BE_LB_ALGO; |
| 1304 | curproxy->lbprm.algo |= BE_LB_ALGO_RR; |
Willy Tarreau | a0cbda6 | 2007-11-01 21:39:54 +0100 | [diff] [blame] | 1305 | return 0; |
| 1306 | } |
| 1307 | |
| 1308 | if (!strcmp(args[0], "roundrobin")) { |
Willy Tarreau | 3168223 | 2007-11-29 15:38:04 +0100 | [diff] [blame] | 1309 | curproxy->lbprm.algo &= ~BE_LB_ALGO; |
| 1310 | curproxy->lbprm.algo |= BE_LB_ALGO_RR; |
Willy Tarreau | a0cbda6 | 2007-11-01 21:39:54 +0100 | [diff] [blame] | 1311 | } |
Willy Tarreau | 9757a38 | 2009-10-03 12:56:50 +0200 | [diff] [blame] | 1312 | else if (!strcmp(args[0], "static-rr")) { |
| 1313 | curproxy->lbprm.algo &= ~BE_LB_ALGO; |
| 1314 | curproxy->lbprm.algo |= BE_LB_ALGO_SRR; |
| 1315 | } |
Willy Tarreau | f09c660 | 2012-02-13 17:12:08 +0100 | [diff] [blame] | 1316 | else if (!strcmp(args[0], "first")) { |
| 1317 | curproxy->lbprm.algo &= ~BE_LB_ALGO; |
| 1318 | curproxy->lbprm.algo |= BE_LB_ALGO_FAS; |
| 1319 | } |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 1320 | else if (!strcmp(args[0], "leastconn")) { |
| 1321 | curproxy->lbprm.algo &= ~BE_LB_ALGO; |
| 1322 | curproxy->lbprm.algo |= BE_LB_ALGO_LC; |
| 1323 | } |
Willy Tarreau | a0cbda6 | 2007-11-01 21:39:54 +0100 | [diff] [blame] | 1324 | else if (!strcmp(args[0], "source")) { |
Willy Tarreau | 3168223 | 2007-11-29 15:38:04 +0100 | [diff] [blame] | 1325 | curproxy->lbprm.algo &= ~BE_LB_ALGO; |
| 1326 | curproxy->lbprm.algo |= BE_LB_ALGO_SH; |
Willy Tarreau | a0cbda6 | 2007-11-01 21:39:54 +0100 | [diff] [blame] | 1327 | } |
| 1328 | else if (!strcmp(args[0], "uri")) { |
Marek Majkowski | 9c30fc1 | 2008-04-27 23:25:55 +0200 | [diff] [blame] | 1329 | int arg = 1; |
| 1330 | |
Willy Tarreau | 3168223 | 2007-11-29 15:38:04 +0100 | [diff] [blame] | 1331 | curproxy->lbprm.algo &= ~BE_LB_ALGO; |
| 1332 | curproxy->lbprm.algo |= BE_LB_ALGO_UH; |
Marek Majkowski | 9c30fc1 | 2008-04-27 23:25:55 +0200 | [diff] [blame] | 1333 | |
Oskar Stolc | 8dc4184 | 2012-05-19 10:19:54 +0100 | [diff] [blame] | 1334 | curproxy->uri_whole = 0; |
| 1335 | |
Marek Majkowski | 9c30fc1 | 2008-04-27 23:25:55 +0200 | [diff] [blame] | 1336 | while (*args[arg]) { |
| 1337 | if (!strcmp(args[arg], "len")) { |
| 1338 | if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) { |
Willy Tarreau | a93c74b | 2012-05-08 18:14:39 +0200 | [diff] [blame] | 1339 | memprintf(err, "%s : '%s' expects a positive integer (got '%s').", args[0], args[arg], args[arg+1]); |
Marek Majkowski | 9c30fc1 | 2008-04-27 23:25:55 +0200 | [diff] [blame] | 1340 | return -1; |
| 1341 | } |
| 1342 | curproxy->uri_len_limit = atoi(args[arg+1]); |
| 1343 | arg += 2; |
| 1344 | } |
| 1345 | else if (!strcmp(args[arg], "depth")) { |
| 1346 | if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) { |
Willy Tarreau | a93c74b | 2012-05-08 18:14:39 +0200 | [diff] [blame] | 1347 | memprintf(err, "%s : '%s' expects a positive integer (got '%s').", args[0], args[arg], args[arg+1]); |
Marek Majkowski | 9c30fc1 | 2008-04-27 23:25:55 +0200 | [diff] [blame] | 1348 | return -1; |
| 1349 | } |
| 1350 | /* hint: we store the position of the ending '/' (depth+1) so |
| 1351 | * that we avoid a comparison while computing the hash. |
| 1352 | */ |
| 1353 | curproxy->uri_dirs_depth1 = atoi(args[arg+1]) + 1; |
| 1354 | arg += 2; |
| 1355 | } |
Oskar Stolc | 8dc4184 | 2012-05-19 10:19:54 +0100 | [diff] [blame] | 1356 | else if (!strcmp(args[arg], "whole")) { |
| 1357 | curproxy->uri_whole = 1; |
| 1358 | arg += 1; |
| 1359 | } |
Marek Majkowski | 9c30fc1 | 2008-04-27 23:25:55 +0200 | [diff] [blame] | 1360 | else { |
Oskar Stolc | 8dc4184 | 2012-05-19 10:19:54 +0100 | [diff] [blame] | 1361 | memprintf(err, "%s only accepts parameters 'len', 'depth', and 'whole' (got '%s').", args[0], args[arg]); |
Marek Majkowski | 9c30fc1 | 2008-04-27 23:25:55 +0200 | [diff] [blame] | 1362 | return -1; |
| 1363 | } |
| 1364 | } |
Willy Tarreau | a0cbda6 | 2007-11-01 21:39:54 +0100 | [diff] [blame] | 1365 | } |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 1366 | else if (!strcmp(args[0], "url_param")) { |
| 1367 | if (!*args[1]) { |
Willy Tarreau | a93c74b | 2012-05-08 18:14:39 +0200 | [diff] [blame] | 1368 | memprintf(err, "%s requires an URL parameter name.", args[0]); |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 1369 | return -1; |
| 1370 | } |
Willy Tarreau | 3168223 | 2007-11-29 15:38:04 +0100 | [diff] [blame] | 1371 | curproxy->lbprm.algo &= ~BE_LB_ALGO; |
| 1372 | curproxy->lbprm.algo |= BE_LB_ALGO_PH; |
Willy Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 1373 | |
| 1374 | free(curproxy->url_param_name); |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 1375 | curproxy->url_param_name = strdup(args[1]); |
Willy Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 1376 | curproxy->url_param_len = strlen(args[1]); |
Marek Majkowski | 9c30fc1 | 2008-04-27 23:25:55 +0200 | [diff] [blame] | 1377 | if (*args[2]) { |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 1378 | if (strcmp(args[2], "check_post")) { |
Willy Tarreau | a93c74b | 2012-05-08 18:14:39 +0200 | [diff] [blame] | 1379 | memprintf(err, "%s only accepts 'check_post' modifier (got '%s').", args[0], args[2]); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 1380 | return -1; |
| 1381 | } |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 1382 | } |
Benoit | affb481 | 2009-03-25 13:02:10 +0100 | [diff] [blame] | 1383 | } |
| 1384 | else if (!strncmp(args[0], "hdr(", 4)) { |
| 1385 | const char *beg, *end; |
| 1386 | |
| 1387 | beg = args[0] + 4; |
| 1388 | end = strchr(beg, ')'); |
| 1389 | |
| 1390 | if (!end || end == beg) { |
Willy Tarreau | a93c74b | 2012-05-08 18:14:39 +0200 | [diff] [blame] | 1391 | memprintf(err, "hdr requires an http header field name."); |
Benoit | affb481 | 2009-03-25 13:02:10 +0100 | [diff] [blame] | 1392 | return -1; |
| 1393 | } |
| 1394 | |
| 1395 | curproxy->lbprm.algo &= ~BE_LB_ALGO; |
| 1396 | curproxy->lbprm.algo |= BE_LB_ALGO_HH; |
| 1397 | |
| 1398 | free(curproxy->hh_name); |
| 1399 | curproxy->hh_len = end - beg; |
| 1400 | curproxy->hh_name = my_strndup(beg, end - beg); |
| 1401 | curproxy->hh_match_domain = 0; |
| 1402 | |
| 1403 | if (*args[1]) { |
| 1404 | if (strcmp(args[1], "use_domain_only")) { |
Willy Tarreau | a93c74b | 2012-05-08 18:14:39 +0200 | [diff] [blame] | 1405 | memprintf(err, "%s only accepts 'use_domain_only' modifier (got '%s').", args[0], args[1]); |
Benoit | affb481 | 2009-03-25 13:02:10 +0100 | [diff] [blame] | 1406 | return -1; |
| 1407 | } |
| 1408 | curproxy->hh_match_domain = 1; |
| 1409 | } |
Emeric Brun | 736aa23 | 2009-06-30 17:56:00 +0200 | [diff] [blame] | 1410 | } |
| 1411 | else if (!strncmp(args[0], "rdp-cookie", 10)) { |
| 1412 | curproxy->lbprm.algo &= ~BE_LB_ALGO; |
| 1413 | curproxy->lbprm.algo |= BE_LB_ALGO_RCH; |
| 1414 | |
| 1415 | if ( *(args[0] + 10 ) == '(' ) { /* cookie name */ |
| 1416 | const char *beg, *end; |
| 1417 | |
| 1418 | beg = args[0] + 11; |
| 1419 | end = strchr(beg, ')'); |
| 1420 | |
| 1421 | if (!end || end == beg) { |
Willy Tarreau | a93c74b | 2012-05-08 18:14:39 +0200 | [diff] [blame] | 1422 | memprintf(err, "rdp-cookie : missing cookie name."); |
Emeric Brun | 736aa23 | 2009-06-30 17:56:00 +0200 | [diff] [blame] | 1423 | return -1; |
| 1424 | } |
| 1425 | |
| 1426 | free(curproxy->hh_name); |
| 1427 | curproxy->hh_name = my_strndup(beg, end - beg); |
| 1428 | curproxy->hh_len = end - beg; |
| 1429 | } |
| 1430 | else if ( *(args[0] + 10 ) == '\0' ) { /* default cookie name 'mstshash' */ |
| 1431 | free(curproxy->hh_name); |
| 1432 | curproxy->hh_name = strdup("mstshash"); |
| 1433 | curproxy->hh_len = strlen(curproxy->hh_name); |
| 1434 | } |
| 1435 | else { /* syntax */ |
Willy Tarreau | a93c74b | 2012-05-08 18:14:39 +0200 | [diff] [blame] | 1436 | memprintf(err, "rdp-cookie : missing cookie name."); |
Emeric Brun | 736aa23 | 2009-06-30 17:56:00 +0200 | [diff] [blame] | 1437 | return -1; |
| 1438 | } |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 1439 | } |
Willy Tarreau | a0cbda6 | 2007-11-01 21:39:54 +0100 | [diff] [blame] | 1440 | else { |
Willy Tarreau | a93c74b | 2012-05-08 18:14:39 +0200 | [diff] [blame] | 1441 | memprintf(err, "only supports 'roundrobin', 'static-rr', 'leastconn', 'source', 'uri', 'url_param', 'hdr(name)' and 'rdp-cookie(name)' options."); |
Willy Tarreau | a0cbda6 | 2007-11-01 21:39:54 +0100 | [diff] [blame] | 1442 | return -1; |
| 1443 | } |
| 1444 | return 0; |
| 1445 | } |
| 1446 | |
Willy Tarreau | a9d3c1e | 2007-11-30 20:48:53 +0100 | [diff] [blame] | 1447 | |
| 1448 | /************************************************************************/ |
Willy Tarreau | 1a7eca1 | 2013-01-07 22:38:03 +0100 | [diff] [blame] | 1449 | /* All supported sample and ACL keywords must be declared here. */ |
Willy Tarreau | a9d3c1e | 2007-11-30 20:48:53 +0100 | [diff] [blame] | 1450 | /************************************************************************/ |
| 1451 | |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 1452 | /* set temp integer to the number of enabled servers on the proxy. |
Willy Tarreau | 0146c2e | 2012-04-20 11:37:56 +0200 | [diff] [blame] | 1453 | * Accepts exactly 1 argument. Argument is a backend, other types will lead to |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 1454 | * undefined behaviour. |
| 1455 | */ |
Willy Tarreau | a9d3c1e | 2007-11-30 20:48:53 +0100 | [diff] [blame] | 1456 | static int |
Willy Tarreau | 1a7eca1 | 2013-01-07 22:38:03 +0100 | [diff] [blame] | 1457 | smp_fetch_nbsrv(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 1458 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | a9d3c1e | 2007-11-30 20:48:53 +0100 | [diff] [blame] | 1459 | { |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 1460 | smp->flags = SMP_F_VOL_TEST; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 1461 | smp->type = SMP_T_UINT; |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 1462 | px = args->data.prx; |
Willy Tarreau | a9d3c1e | 2007-11-30 20:48:53 +0100 | [diff] [blame] | 1463 | |
| 1464 | if (px->srv_act) |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 1465 | smp->data.uint = px->srv_act; |
Willy Tarreau | a9d3c1e | 2007-11-30 20:48:53 +0100 | [diff] [blame] | 1466 | else if (px->lbprm.fbck) |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 1467 | smp->data.uint = 1; |
Willy Tarreau | a9d3c1e | 2007-11-30 20:48:53 +0100 | [diff] [blame] | 1468 | else |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 1469 | smp->data.uint = px->srv_bck; |
Willy Tarreau | a9d3c1e | 2007-11-30 20:48:53 +0100 | [diff] [blame] | 1470 | |
| 1471 | return 1; |
| 1472 | } |
| 1473 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 1474 | /* report in smp->flags a success or failure depending on the designated |
Willy Tarreau | 0b1cd94 | 2010-05-16 22:18:27 +0200 | [diff] [blame] | 1475 | * server's state. There is no match function involved since there's no pattern. |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 1476 | * Accepts exactly 1 argument. Argument is a server, other types will lead to |
| 1477 | * undefined behaviour. |
Willy Tarreau | 0b1cd94 | 2010-05-16 22:18:27 +0200 | [diff] [blame] | 1478 | */ |
| 1479 | static int |
Willy Tarreau | 1a7eca1 | 2013-01-07 22:38:03 +0100 | [diff] [blame] | 1480 | smp_fetch_srv_is_up(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 1481 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 0b1cd94 | 2010-05-16 22:18:27 +0200 | [diff] [blame] | 1482 | { |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 1483 | struct server *srv = args->data.srv; |
Willy Tarreau | 0b1cd94 | 2010-05-16 22:18:27 +0200 | [diff] [blame] | 1484 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 1485 | smp->flags = SMP_F_VOL_TEST; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 1486 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 0b1cd94 | 2010-05-16 22:18:27 +0200 | [diff] [blame] | 1487 | if (!(srv->state & SRV_MAINTAIN) && |
Willy Tarreau | ff5ae35 | 2013-12-11 20:36:34 +0100 | [diff] [blame] | 1488 | (!(srv->check.state & CHK_ST_CONFIGURED) || (srv->state & SRV_RUNNING))) |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 1489 | smp->data.uint = 1; |
Willy Tarreau | 0b1cd94 | 2010-05-16 22:18:27 +0200 | [diff] [blame] | 1490 | else |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 1491 | smp->data.uint = 0; |
Willy Tarreau | 0b1cd94 | 2010-05-16 22:18:27 +0200 | [diff] [blame] | 1492 | return 1; |
| 1493 | } |
| 1494 | |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 1495 | /* set temp integer to the number of enabled servers on the proxy. |
Willy Tarreau | 0146c2e | 2012-04-20 11:37:56 +0200 | [diff] [blame] | 1496 | * Accepts exactly 1 argument. Argument is a backend, other types will lead to |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 1497 | * undefined behaviour. |
| 1498 | */ |
Jeffrey 'jf' Lim | 5051d7b | 2008-09-04 01:03:03 +0800 | [diff] [blame] | 1499 | static int |
Willy Tarreau | 1a7eca1 | 2013-01-07 22:38:03 +0100 | [diff] [blame] | 1500 | smp_fetch_connslots(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 1501 | const struct arg *args, struct sample *smp, const char *kw) |
Jeffrey 'jf' Lim | 5051d7b | 2008-09-04 01:03:03 +0800 | [diff] [blame] | 1502 | { |
| 1503 | struct server *iterator; |
Willy Tarreau | d28c353 | 2012-04-19 19:28:33 +0200 | [diff] [blame] | 1504 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 1505 | smp->flags = SMP_F_VOL_TEST; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 1506 | smp->type = SMP_T_UINT; |
| 1507 | smp->data.uint = 0; |
Willy Tarreau | 0146c2e | 2012-04-20 11:37:56 +0200 | [diff] [blame] | 1508 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 1509 | for (iterator = args->data.prx->srv; iterator; iterator = iterator->next) { |
Willy Tarreau | 0146c2e | 2012-04-20 11:37:56 +0200 | [diff] [blame] | 1510 | if ((iterator->state & SRV_RUNNING) == 0) |
Jeffrey 'jf' Lim | 5051d7b | 2008-09-04 01:03:03 +0800 | [diff] [blame] | 1511 | continue; |
Willy Tarreau | 0146c2e | 2012-04-20 11:37:56 +0200 | [diff] [blame] | 1512 | |
Jeffrey 'jf' Lim | 5051d7b | 2008-09-04 01:03:03 +0800 | [diff] [blame] | 1513 | if (iterator->maxconn == 0 || iterator->maxqueue == 0) { |
Willy Tarreau | a5e3756 | 2011-12-16 17:06:15 +0100 | [diff] [blame] | 1514 | /* configuration is stupid */ |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 1515 | smp->data.uint = -1; /* FIXME: stupid value! */ |
Jeffrey 'jf' Lim | 5051d7b | 2008-09-04 01:03:03 +0800 | [diff] [blame] | 1516 | return 1; |
| 1517 | } |
| 1518 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 1519 | smp->data.uint += (iterator->maxconn - iterator->cur_sess) |
Willy Tarreau | 422aa07 | 2012-04-20 20:49:27 +0200 | [diff] [blame] | 1520 | + (iterator->maxqueue - iterator->nbpend); |
Jeffrey 'jf' Lim | 5051d7b | 2008-09-04 01:03:03 +0800 | [diff] [blame] | 1521 | } |
| 1522 | |
| 1523 | return 1; |
| 1524 | } |
| 1525 | |
Willy Tarreau | a5e3756 | 2011-12-16 17:06:15 +0100 | [diff] [blame] | 1526 | /* set temp integer to the id of the backend */ |
Hervé COMMOWICK | 35ed801 | 2010-12-15 14:04:51 +0100 | [diff] [blame] | 1527 | static int |
Willy Tarreau | 1a7eca1 | 2013-01-07 22:38:03 +0100 | [diff] [blame] | 1528 | smp_fetch_be_id(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 1529 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 1530 | { |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 1531 | smp->flags = SMP_F_VOL_TXN; |
| 1532 | smp->type = SMP_T_UINT; |
| 1533 | smp->data.uint = l4->be->uuid; |
Hervé COMMOWICK | 35ed801 | 2010-12-15 14:04:51 +0100 | [diff] [blame] | 1534 | return 1; |
| 1535 | } |
| 1536 | |
Willy Tarreau | a5e3756 | 2011-12-16 17:06:15 +0100 | [diff] [blame] | 1537 | /* set temp integer to the id of the server */ |
Hervé COMMOWICK | 35ed801 | 2010-12-15 14:04:51 +0100 | [diff] [blame] | 1538 | static int |
Willy Tarreau | 1a7eca1 | 2013-01-07 22:38:03 +0100 | [diff] [blame] | 1539 | smp_fetch_srv_id(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 1540 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 1541 | { |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 1542 | if (!objt_server(l4->target)) |
Willy Tarreau | 17af419 | 2011-02-23 14:27:06 +0100 | [diff] [blame] | 1543 | return 0; |
| 1544 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 1545 | smp->type = SMP_T_UINT; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 1546 | smp->data.uint = objt_server(l4->target)->puid; |
Hervé COMMOWICK | 35ed801 | 2010-12-15 14:04:51 +0100 | [diff] [blame] | 1547 | |
| 1548 | return 1; |
| 1549 | } |
| 1550 | |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 1551 | /* set temp integer to the number of connections per second reaching the backend. |
Willy Tarreau | 0146c2e | 2012-04-20 11:37:56 +0200 | [diff] [blame] | 1552 | * Accepts exactly 1 argument. Argument is a backend, other types will lead to |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 1553 | * undefined behaviour. |
| 1554 | */ |
Willy Tarreau | 079ff0a | 2009-03-05 21:34:28 +0100 | [diff] [blame] | 1555 | static int |
Willy Tarreau | 1a7eca1 | 2013-01-07 22:38:03 +0100 | [diff] [blame] | 1556 | smp_fetch_be_sess_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 1557 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 079ff0a | 2009-03-05 21:34:28 +0100 | [diff] [blame] | 1558 | { |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 1559 | smp->flags = SMP_F_VOL_TEST; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 1560 | smp->type = SMP_T_UINT; |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 1561 | smp->data.uint = read_freq_ctr(&args->data.prx->be_sess_per_sec); |
Willy Tarreau | 079ff0a | 2009-03-05 21:34:28 +0100 | [diff] [blame] | 1562 | return 1; |
| 1563 | } |
| 1564 | |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 1565 | /* set temp integer to the number of concurrent connections on the backend. |
Willy Tarreau | 0146c2e | 2012-04-20 11:37:56 +0200 | [diff] [blame] | 1566 | * Accepts exactly 1 argument. Argument is a backend, other types will lead to |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 1567 | * undefined behaviour. |
| 1568 | */ |
Willy Tarreau | a36af91 | 2009-10-10 12:02:45 +0200 | [diff] [blame] | 1569 | static int |
Willy Tarreau | 1a7eca1 | 2013-01-07 22:38:03 +0100 | [diff] [blame] | 1570 | smp_fetch_be_conn(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 1571 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | a36af91 | 2009-10-10 12:02:45 +0200 | [diff] [blame] | 1572 | { |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 1573 | smp->flags = SMP_F_VOL_TEST; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 1574 | smp->type = SMP_T_UINT; |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 1575 | smp->data.uint = args->data.prx->beconn; |
Willy Tarreau | a36af91 | 2009-10-10 12:02:45 +0200 | [diff] [blame] | 1576 | return 1; |
| 1577 | } |
| 1578 | |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 1579 | /* set temp integer to the total number of queued connections on the backend. |
Willy Tarreau | 0146c2e | 2012-04-20 11:37:56 +0200 | [diff] [blame] | 1580 | * Accepts exactly 1 argument. Argument is a backend, other types will lead to |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 1581 | * undefined behaviour. |
| 1582 | */ |
Willy Tarreau | a36af91 | 2009-10-10 12:02:45 +0200 | [diff] [blame] | 1583 | static int |
Willy Tarreau | 1a7eca1 | 2013-01-07 22:38:03 +0100 | [diff] [blame] | 1584 | smp_fetch_queue_size(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 1585 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | a36af91 | 2009-10-10 12:02:45 +0200 | [diff] [blame] | 1586 | { |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 1587 | smp->flags = SMP_F_VOL_TEST; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 1588 | smp->type = SMP_T_UINT; |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 1589 | smp->data.uint = args->data.prx->totpend; |
Willy Tarreau | a36af91 | 2009-10-10 12:02:45 +0200 | [diff] [blame] | 1590 | return 1; |
| 1591 | } |
| 1592 | |
Willy Tarreau | a5e3756 | 2011-12-16 17:06:15 +0100 | [diff] [blame] | 1593 | /* set temp integer to the total number of queued connections on the backend divided |
Willy Tarreau | a36af91 | 2009-10-10 12:02:45 +0200 | [diff] [blame] | 1594 | * by the number of running servers and rounded up. If there is no running |
| 1595 | * server, we return twice the total, just as if we had half a running server. |
| 1596 | * This is more or less correct anyway, since we expect the last server to come |
| 1597 | * back soon. |
Willy Tarreau | 0146c2e | 2012-04-20 11:37:56 +0200 | [diff] [blame] | 1598 | * Accepts exactly 1 argument. Argument is a backend, other types will lead to |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 1599 | * undefined behaviour. |
Willy Tarreau | a36af91 | 2009-10-10 12:02:45 +0200 | [diff] [blame] | 1600 | */ |
| 1601 | static int |
Willy Tarreau | 1a7eca1 | 2013-01-07 22:38:03 +0100 | [diff] [blame] | 1602 | smp_fetch_avg_queue_size(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 1603 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | a36af91 | 2009-10-10 12:02:45 +0200 | [diff] [blame] | 1604 | { |
| 1605 | int nbsrv; |
| 1606 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 1607 | smp->flags = SMP_F_VOL_TEST; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 1608 | smp->type = SMP_T_UINT; |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 1609 | px = args->data.prx; |
Willy Tarreau | a36af91 | 2009-10-10 12:02:45 +0200 | [diff] [blame] | 1610 | |
| 1611 | if (px->srv_act) |
| 1612 | nbsrv = px->srv_act; |
| 1613 | else if (px->lbprm.fbck) |
| 1614 | nbsrv = 1; |
| 1615 | else |
| 1616 | nbsrv = px->srv_bck; |
| 1617 | |
| 1618 | if (nbsrv > 0) |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 1619 | smp->data.uint = (px->totpend + nbsrv - 1) / nbsrv; |
Willy Tarreau | a36af91 | 2009-10-10 12:02:45 +0200 | [diff] [blame] | 1620 | else |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 1621 | smp->data.uint = px->totpend * 2; |
Willy Tarreau | a36af91 | 2009-10-10 12:02:45 +0200 | [diff] [blame] | 1622 | |
| 1623 | return 1; |
| 1624 | } |
Willy Tarreau | a9d3c1e | 2007-11-30 20:48:53 +0100 | [diff] [blame] | 1625 | |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 1626 | /* set temp integer to the number of concurrent connections on the server in the backend. |
| 1627 | * Accepts exactly 1 argument. Argument is a server, other types will lead to |
| 1628 | * undefined behaviour. |
| 1629 | */ |
Hervé COMMOWICK | daa824e | 2011-08-05 12:09:44 +0200 | [diff] [blame] | 1630 | static int |
Willy Tarreau | 1a7eca1 | 2013-01-07 22:38:03 +0100 | [diff] [blame] | 1631 | smp_fetch_srv_conn(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 1632 | const struct arg *args, struct sample *smp, const char *kw) |
Hervé COMMOWICK | daa824e | 2011-08-05 12:09:44 +0200 | [diff] [blame] | 1633 | { |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 1634 | smp->flags = SMP_F_VOL_TEST; |
| 1635 | smp->type = SMP_T_UINT; |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 1636 | smp->data.uint = args->data.srv->cur_sess; |
Hervé COMMOWICK | daa824e | 2011-08-05 12:09:44 +0200 | [diff] [blame] | 1637 | return 1; |
| 1638 | } |
| 1639 | |
Tait Clarridge | 7896d52 | 2012-12-05 21:39:31 -0500 | [diff] [blame] | 1640 | /* set temp integer to the number of enabled servers on the proxy. |
| 1641 | * Accepts exactly 1 argument. Argument is a server, other types will lead to |
| 1642 | * undefined behaviour. |
| 1643 | */ |
| 1644 | static int |
Willy Tarreau | 1a7eca1 | 2013-01-07 22:38:03 +0100 | [diff] [blame] | 1645 | smp_fetch_srv_sess_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 1646 | const struct arg *args, struct sample *smp, const char *kw) |
Tait Clarridge | 7896d52 | 2012-12-05 21:39:31 -0500 | [diff] [blame] | 1647 | { |
| 1648 | smp->flags = SMP_F_VOL_TEST; |
| 1649 | smp->type = SMP_T_UINT; |
| 1650 | smp->data.uint = read_freq_ctr(&args->data.srv->sess_per_sec); |
| 1651 | return 1; |
| 1652 | } |
| 1653 | |
Willy Tarreau | 1a7eca1 | 2013-01-07 22:38:03 +0100 | [diff] [blame] | 1654 | |
| 1655 | /* Note: must not be declared <const> as its list will be overwritten. |
| 1656 | * Please take care of keeping this list alphabetically sorted. |
| 1657 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 1658 | static struct sample_fetch_kw_list smp_kws = {ILH, { |
Willy Tarreau | 1a7eca1 | 2013-01-07 22:38:03 +0100 | [diff] [blame] | 1659 | { "avg_queue", smp_fetch_avg_queue_size, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, }, |
| 1660 | { "be_conn", smp_fetch_be_conn, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, }, |
| 1661 | { "be_id", smp_fetch_be_id, 0, NULL, SMP_T_UINT, SMP_USE_BKEND, }, |
| 1662 | { "be_sess_rate", smp_fetch_be_sess_rate, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, }, |
| 1663 | { "connslots", smp_fetch_connslots, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, }, |
| 1664 | { "nbsrv", smp_fetch_nbsrv, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, }, |
| 1665 | { "queue", smp_fetch_queue_size, ARG1(1,BE), NULL, SMP_T_UINT, SMP_USE_INTRN, }, |
| 1666 | { "srv_conn", smp_fetch_srv_conn, ARG1(1,SRV), NULL, SMP_T_UINT, SMP_USE_INTRN, }, |
| 1667 | { "srv_id", smp_fetch_srv_id, 0, NULL, SMP_T_UINT, SMP_USE_SERVR, }, |
| 1668 | { "srv_is_up", smp_fetch_srv_is_up, ARG1(1,SRV), NULL, SMP_T_BOOL, SMP_USE_INTRN, }, |
| 1669 | { "srv_sess_rate", smp_fetch_srv_sess_rate, ARG1(1,SRV), NULL, SMP_T_UINT, SMP_USE_INTRN, }, |
| 1670 | { /* END */ }, |
| 1671 | }}; |
| 1672 | |
| 1673 | |
Willy Tarreau | 61612d4 | 2012-04-19 18:42:05 +0200 | [diff] [blame] | 1674 | /* Note: must not be declared <const> as its list will be overwritten. |
| 1675 | * Please take care of keeping this list alphabetically sorted. |
| 1676 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 1677 | static struct acl_kw_list acl_kws = {ILH, { |
Willy Tarreau | 1a7eca1 | 2013-01-07 22:38:03 +0100 | [diff] [blame] | 1678 | { /* END */ }, |
Willy Tarreau | a9d3c1e | 2007-11-30 20:48:53 +0100 | [diff] [blame] | 1679 | }}; |
| 1680 | |
| 1681 | |
| 1682 | __attribute__((constructor)) |
| 1683 | static void __backend_init(void) |
| 1684 | { |
Willy Tarreau | 1a7eca1 | 2013-01-07 22:38:03 +0100 | [diff] [blame] | 1685 | sample_register_fetches(&smp_kws); |
Willy Tarreau | a9d3c1e | 2007-11-30 20:48:53 +0100 | [diff] [blame] | 1686 | acl_register_keywords(&acl_kws); |
| 1687 | } |
| 1688 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1689 | /* |
| 1690 | * Local variables: |
| 1691 | * c-indent-level: 8 |
| 1692 | * c-basic-offset: 8 |
| 1693 | * End: |
| 1694 | */ |