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