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