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