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 | |
| 664 | /* if this server remaps proxied ports, we'll use |
| 665 | * the port the client connected to with an offset. */ |
| 666 | if (s->srv->state & SRV_MAPPORTS) { |
Willy Tarreau | 4b1f859 | 2008-12-23 23:13:55 +0100 | [diff] [blame] | 667 | 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] | 668 | get_frt_addr(s); |
| 669 | if (s->frt_addr.ss_family == AF_INET) { |
| 670 | s->srv_addr.sin_port = htons(ntohs(s->srv_addr.sin_port) + |
| 671 | ntohs(((struct sockaddr_in *)&s->frt_addr)->sin_port)); |
| 672 | } else { |
| 673 | s->srv_addr.sin_port = htons(ntohs(s->srv_addr.sin_port) + |
| 674 | ntohs(((struct sockaddr_in6 *)&s->frt_addr)->sin6_port)); |
| 675 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 676 | } |
| 677 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 678 | else if (*(int *)&s->be->dispatch_addr.sin_addr) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 679 | /* connect to the defined dispatch addr */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 680 | s->srv_addr = s->be->dispatch_addr; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 681 | } |
Willy Tarreau | 4b1f859 | 2008-12-23 23:13:55 +0100 | [diff] [blame] | 682 | else if (s->be->options & PR_O_TRANSP) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 683 | /* in transparent mode, use the original dest addr if no dispatch specified */ |
Willy Tarreau | bd41428 | 2008-01-19 13:46:35 +0100 | [diff] [blame] | 684 | if (!(s->flags & SN_FRT_ADDR_SET)) |
| 685 | get_frt_addr(s); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 686 | |
Willy Tarreau | bd41428 | 2008-01-19 13:46:35 +0100 | [diff] [blame] | 687 | memcpy(&s->srv_addr, &s->frt_addr, MIN(sizeof(s->srv_addr), sizeof(s->frt_addr))); |
| 688 | /* when we support IPv6 on the backend, we may add other tests */ |
| 689 | //qfprintf(stderr, "Cannot get original server address.\n"); |
| 690 | //return SRV_STATUS_INTERNAL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 691 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 692 | else if (s->be->options & PR_O_HTTP_PROXY) { |
| 693 | /* If HTTP PROXY option is set, then server is already assigned |
| 694 | * during incoming client request parsing. */ |
| 695 | } |
Willy Tarreau | 1a1158b | 2007-01-20 11:07:46 +0100 | [diff] [blame] | 696 | else { |
| 697 | /* no server and no LB algorithm ! */ |
| 698 | return SRV_STATUS_INTERNAL; |
| 699 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 700 | |
| 701 | s->flags |= SN_ADDR_SET; |
| 702 | return SRV_STATUS_OK; |
| 703 | } |
| 704 | |
| 705 | |
| 706 | /* This function assigns a server to session <s> if required, and can add the |
| 707 | * 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] | 708 | * If ->srv_conn is set, the session is first released from the server. |
| 709 | * It may also be called with SN_DIRECT and/or SN_ASSIGNED though. It will |
| 710 | * be called before any connection and after any retry or redispatch occurs. |
| 711 | * |
| 712 | * 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] | 713 | * |
| 714 | * Returns : |
| 715 | * |
| 716 | * SRV_STATUS_OK if everything is OK. |
| 717 | * SRV_STATUS_NOSRV if no server is available. s->srv = NULL. |
| 718 | * SRV_STATUS_QUEUED if the connection has been queued. |
| 719 | * SRV_STATUS_FULL if the server(s) is/are saturated and the |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 720 | * connection could not be queued in s->srv, |
| 721 | * which may be NULL if we queue on the backend. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 722 | * SRV_STATUS_INTERNAL for other unrecoverable errors. |
| 723 | * |
| 724 | */ |
| 725 | int assign_server_and_queue(struct session *s) |
| 726 | { |
| 727 | struct pendconn *p; |
| 728 | int err; |
| 729 | |
| 730 | if (s->pend_pos) |
| 731 | return SRV_STATUS_INTERNAL; |
| 732 | |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 733 | err = SRV_STATUS_OK; |
| 734 | if (!(s->flags & SN_ASSIGNED)) { |
| 735 | err = assign_server(s); |
| 736 | if (s->prev_srv) { |
| 737 | /* This session was previously assigned to a server. We have to |
| 738 | * update the session's and the server's stats : |
| 739 | * - if the server changed : |
| 740 | * - set TX_CK_DOWN if txn.flags was TX_CK_VALID |
| 741 | * - set SN_REDISP if it was successfully redispatched |
| 742 | * - increment srv->redispatches and be->redispatches |
| 743 | * - if the server remained the same : update retries. |
Krzysztof Piotr Oledzki | 5a329cf | 2008-02-22 03:50:19 +0100 | [diff] [blame] | 744 | */ |
| 745 | |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 746 | if (s->prev_srv != s->srv) { |
| 747 | if ((s->txn.flags & TX_CK_MASK) == TX_CK_VALID) { |
| 748 | s->txn.flags &= ~TX_CK_MASK; |
| 749 | s->txn.flags |= TX_CK_DOWN; |
| 750 | } |
| 751 | s->flags |= SN_REDISP; |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 752 | s->prev_srv->counters.redispatches++; |
| 753 | s->be->counters.redispatches++; |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 754 | } else { |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 755 | s->prev_srv->counters.retries++; |
| 756 | s->be->counters.retries++; |
Krzysztof Piotr Oledzki | 5a329cf | 2008-02-22 03:50:19 +0100 | [diff] [blame] | 757 | } |
Krzysztof Piotr Oledzki | 5a329cf | 2008-02-22 03:50:19 +0100 | [diff] [blame] | 758 | } |
| 759 | } |
| 760 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 761 | switch (err) { |
| 762 | case SRV_STATUS_OK: |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 763 | /* we have SN_ASSIGNED set */ |
| 764 | if (!s->srv) |
| 765 | return SRV_STATUS_OK; /* dispatch or proxy mode */ |
| 766 | |
| 767 | /* If we already have a connection slot, no need to check any queue */ |
| 768 | if (s->srv_conn == s->srv) |
| 769 | return SRV_STATUS_OK; |
| 770 | |
| 771 | /* OK, this session already has an assigned server, but no |
| 772 | * connection slot yet. Either it is a redispatch, or it was |
| 773 | * assigned from persistence information (direct mode). |
| 774 | */ |
| 775 | if ((s->flags & SN_REDIRECTABLE) && s->srv->rdr_len) { |
| 776 | /* server scheduled for redirection, and already assigned. We |
| 777 | * don't want to go further nor check the queue. |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 778 | */ |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 779 | sess_change_server(s, s->srv); /* not really needed in fact */ |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 780 | return SRV_STATUS_OK; |
| 781 | } |
| 782 | |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 783 | /* We might have to queue this session if the assigned server is full. |
| 784 | * We know we have to queue it into the server's queue, so if a maxqueue |
| 785 | * is set on the server, we must also check that the server's queue is |
| 786 | * not full, in which case we have to return FULL. |
| 787 | */ |
| 788 | if (s->srv->maxconn && |
| 789 | (s->srv->nbpend || s->srv->served >= srv_dynamic_maxconn(s->srv))) { |
| 790 | |
| 791 | if (s->srv->maxqueue > 0 && s->srv->nbpend >= s->srv->maxqueue) |
| 792 | return SRV_STATUS_FULL; |
| 793 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 794 | p = pendconn_add(s); |
| 795 | if (p) |
| 796 | return SRV_STATUS_QUEUED; |
| 797 | else |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 798 | return SRV_STATUS_INTERNAL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 799 | } |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 800 | |
| 801 | /* OK, we can use this server. Let's reserve our place */ |
| 802 | sess_change_server(s, s->srv); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 803 | return SRV_STATUS_OK; |
| 804 | |
| 805 | case SRV_STATUS_FULL: |
| 806 | /* queue this session into the proxy's queue */ |
| 807 | p = pendconn_add(s); |
| 808 | if (p) |
| 809 | return SRV_STATUS_QUEUED; |
| 810 | else |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 811 | return SRV_STATUS_INTERNAL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 812 | |
| 813 | case SRV_STATUS_NOSRV: |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 814 | return err; |
| 815 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 816 | case SRV_STATUS_INTERNAL: |
| 817 | return err; |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 818 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 819 | default: |
| 820 | return SRV_STATUS_INTERNAL; |
| 821 | } |
Willy Tarreau | 5b6995c | 2008-01-13 16:31:17 +0100 | [diff] [blame] | 822 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 823 | |
Willy Tarreau | b1d6774 | 2010-03-29 19:36:59 +0200 | [diff] [blame] | 824 | /* If an explicit source binding is specified on the server and/or backend, and |
| 825 | * this source makes use of the transparent proxy, then it is extracted now and |
| 826 | * assigned to the session's from_addr entry. |
| 827 | */ |
| 828 | static void assign_tproxy_address(struct session *s) |
| 829 | { |
| 830 | #if defined(CONFIG_HAP_CTTPROXY) || defined(CONFIG_HAP_LINUX_TPROXY) |
| 831 | if (s->srv != NULL && s->srv->state & SRV_BIND_SRC) { |
| 832 | switch (s->srv->state & SRV_TPROXY_MASK) { |
| 833 | case SRV_TPROXY_ADDR: |
| 834 | s->from_addr = *(struct sockaddr_in *)&s->srv->tproxy_addr; |
| 835 | break; |
| 836 | case SRV_TPROXY_CLI: |
| 837 | case SRV_TPROXY_CIP: |
| 838 | /* FIXME: what can we do if the client connects in IPv6 ? */ |
| 839 | s->from_addr = *(struct sockaddr_in *)&s->cli_addr; |
| 840 | break; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 841 | case SRV_TPROXY_DYN: |
| 842 | if (s->srv->bind_hdr_occ) { |
| 843 | /* bind to the IP in a header */ |
| 844 | s->from_addr.sin_port = 0; |
| 845 | s->from_addr.sin_addr.s_addr = htonl(get_ip_from_hdr2(&s->txn.req, |
| 846 | s->srv->bind_hdr_name, |
| 847 | s->srv->bind_hdr_len, |
| 848 | &s->txn.hdr_idx, |
| 849 | s->srv->bind_hdr_occ)); |
| 850 | } |
| 851 | break; |
Willy Tarreau | b1d6774 | 2010-03-29 19:36:59 +0200 | [diff] [blame] | 852 | default: |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 853 | memset(&s->from_addr, 0, sizeof(s->from_addr)); |
Willy Tarreau | b1d6774 | 2010-03-29 19:36:59 +0200 | [diff] [blame] | 854 | } |
| 855 | } |
| 856 | else if (s->be->options & PR_O_BIND_SRC) { |
| 857 | switch (s->be->options & PR_O_TPXY_MASK) { |
| 858 | case PR_O_TPXY_ADDR: |
| 859 | s->from_addr = *(struct sockaddr_in *)&s->be->tproxy_addr; |
| 860 | break; |
| 861 | case PR_O_TPXY_CLI: |
| 862 | case PR_O_TPXY_CIP: |
| 863 | /* FIXME: what can we do if the client connects in IPv6 ? */ |
| 864 | s->from_addr = *(struct sockaddr_in *)&s->cli_addr; |
| 865 | break; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 866 | case PR_O_TPXY_DYN: |
| 867 | if (s->be->bind_hdr_occ) { |
| 868 | /* bind to the IP in a header */ |
| 869 | s->from_addr.sin_port = 0; |
| 870 | s->from_addr.sin_addr.s_addr = htonl(get_ip_from_hdr2(&s->txn.req, |
| 871 | s->be->bind_hdr_name, |
| 872 | s->be->bind_hdr_len, |
| 873 | &s->txn.hdr_idx, |
| 874 | s->be->bind_hdr_occ)); |
| 875 | } |
| 876 | break; |
Willy Tarreau | b1d6774 | 2010-03-29 19:36:59 +0200 | [diff] [blame] | 877 | default: |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 878 | memset(&s->from_addr, 0, sizeof(s->from_addr)); |
Willy Tarreau | b1d6774 | 2010-03-29 19:36:59 +0200 | [diff] [blame] | 879 | } |
| 880 | } |
| 881 | #endif |
| 882 | } |
| 883 | |
| 884 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 885 | /* |
| 886 | * This function initiates a connection to the server assigned to this session |
| 887 | * (s->srv, s->srv_addr). It will assign a server if none is assigned yet. |
| 888 | * It can return one of : |
| 889 | * - SN_ERR_NONE if everything's OK |
| 890 | * - SN_ERR_SRVTO if there are no more servers |
| 891 | * - SN_ERR_SRVCL if the connection was refused by the server |
| 892 | * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn) |
| 893 | * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...) |
| 894 | * - SN_ERR_INTERNAL for any other purely internal errors |
| 895 | * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted. |
| 896 | */ |
| 897 | int connect_server(struct session *s) |
| 898 | { |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 899 | int err; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 900 | |
| 901 | if (!(s->flags & SN_ADDR_SET)) { |
| 902 | err = assign_server_address(s); |
| 903 | if (err != SRV_STATUS_OK) |
| 904 | return SN_ERR_INTERNAL; |
| 905 | } |
| 906 | |
Willy Tarreau | a8f55d5 | 2010-05-31 17:44:19 +0200 | [diff] [blame] | 907 | /* Prepare the stream interface for a TCP connection. Later |
| 908 | * we may assign a protocol-specific connect() function. |
| 909 | */ |
| 910 | stream_sock_prepare_interface(s->req->cons); |
| 911 | s->req->cons->connect = tcpv4_connect_server; |
Willy Tarreau | d88edf2 | 2009-06-14 15:48:17 +0200 | [diff] [blame] | 912 | |
Willy Tarreau | b1d6774 | 2010-03-29 19:36:59 +0200 | [diff] [blame] | 913 | assign_tproxy_address(s); |
| 914 | |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 915 | err = s->req->cons->connect(s->req->cons, s->be, s->srv, |
| 916 | (struct sockaddr *)&s->srv_addr, |
Willy Tarreau | b1d6774 | 2010-03-29 19:36:59 +0200 | [diff] [blame] | 917 | (struct sockaddr *)&s->from_addr); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 918 | |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 919 | if (err != SN_ERR_NONE) |
| 920 | return err; |
Willy Tarreau | 788e284 | 2008-08-26 13:25:39 +0200 | [diff] [blame] | 921 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 922 | if (s->srv) { |
Willy Tarreau | 1e62de6 | 2008-11-11 20:20:02 +0100 | [diff] [blame] | 923 | s->flags |= SN_CURR_SESS; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 924 | s->srv->cur_sess++; |
Willy Tarreau | ac68c5d | 2009-10-04 23:12:44 +0200 | [diff] [blame] | 925 | if (s->srv->cur_sess > s->srv->counters.cur_sess_max) |
| 926 | s->srv->counters.cur_sess_max = s->srv->cur_sess; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 927 | if (s->be->lbprm.server_take_conn) |
| 928 | s->be->lbprm.server_take_conn(s->srv); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 929 | } |
| 930 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 931 | return SN_ERR_NONE; /* connection is OK */ |
| 932 | } |
| 933 | |
| 934 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 935 | /* This function performs the "redispatch" part of a connection attempt. It |
| 936 | * will assign a server if required, queue the connection if required, and |
| 937 | * handle errors that might arise at this level. It can change the server |
| 938 | * state. It will return 1 if it encounters an error, switches the server |
| 939 | * state, or has to queue a connection. Otherwise, it will return 0 indicating |
| 940 | * that the connection is ready to use. |
| 941 | */ |
| 942 | |
| 943 | int srv_redispatch_connect(struct session *t) |
| 944 | { |
| 945 | int conn_err; |
| 946 | |
| 947 | /* We know that we don't have any connection pending, so we will |
| 948 | * try to get a new one, and wait in this state if it's queued |
| 949 | */ |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 950 | redispatch: |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 951 | conn_err = assign_server_and_queue(t); |
| 952 | switch (conn_err) { |
| 953 | case SRV_STATUS_OK: |
| 954 | break; |
| 955 | |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 956 | case SRV_STATUS_FULL: |
| 957 | /* The server has reached its maxqueue limit. Either PR_O_REDISP is set |
| 958 | * and we can redispatch to another server, or it is not and we return |
| 959 | * 503. This only makes sense in DIRECT mode however, because normal LB |
| 960 | * algorithms would never select such a server, and hash algorithms |
| 961 | * would bring us on the same server again. Note that t->srv is set in |
| 962 | * this case. |
| 963 | */ |
Willy Tarreau | 4de9149 | 2010-01-22 19:10:05 +0100 | [diff] [blame] | 964 | if (((t->flags & (SN_DIRECT|SN_FORCE_PRST)) == SN_DIRECT) && |
| 965 | (t->be->options & PR_O_REDISP)) { |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 966 | t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET); |
| 967 | t->prev_srv = t->srv; |
| 968 | goto redispatch; |
| 969 | } |
| 970 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 971 | if (!t->req->cons->err_type) { |
| 972 | t->req->cons->err_type = SI_ET_QUEUE_ERR; |
| 973 | t->req->cons->err_loc = t->srv; |
| 974 | } |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 975 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 976 | t->srv->counters.failed_conns++; |
| 977 | t->be->counters.failed_conns++; |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 978 | return 1; |
| 979 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 980 | case SRV_STATUS_NOSRV: |
| 981 | /* note: it is guaranteed that t->srv == NULL here */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 982 | if (!t->req->cons->err_type) { |
| 983 | t->req->cons->err_type = SI_ET_CONN_ERR; |
| 984 | t->req->cons->err_loc = NULL; |
| 985 | } |
Krzysztof Piotr Oledzki | 5a329cf | 2008-02-22 03:50:19 +0100 | [diff] [blame] | 986 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 987 | t->be->counters.failed_conns++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 988 | return 1; |
| 989 | |
| 990 | case SRV_STATUS_QUEUED: |
Willy Tarreau | 3537467 | 2008-09-03 18:11:02 +0200 | [diff] [blame] | 991 | 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] | 992 | t->req->cons->state = SI_ST_QUE; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 993 | /* do nothing else and do not wake any other session up */ |
| 994 | return 1; |
| 995 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 996 | case SRV_STATUS_INTERNAL: |
| 997 | default: |
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_OTHER; |
| 1000 | t->req->cons->err_loc = t->srv; |
| 1001 | } |
| 1002 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1003 | if (t->srv) |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 1004 | srv_inc_sess_ctr(t->srv); |
Willy Tarreau | 98937b8 | 2007-12-10 15:05:42 +0100 | [diff] [blame] | 1005 | if (t->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 1006 | t->srv->counters.failed_conns++; |
| 1007 | t->be->counters.failed_conns++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1008 | |
| 1009 | /* release other sessions waiting for this server */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1010 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1011 | process_srv_queue(t->srv); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1012 | return 1; |
| 1013 | } |
| 1014 | /* if we get here, it's because we got SRV_STATUS_OK, which also |
| 1015 | * means that the connection has not been queued. |
| 1016 | */ |
| 1017 | return 0; |
| 1018 | } |
| 1019 | |
Willy Tarreau | 44b90cc | 2010-05-24 20:27:29 +0200 | [diff] [blame] | 1020 | /* Apply RDP cookie persistence to the current session. For this, the function |
| 1021 | * tries to extract an RDP cookie from the request buffer, and look for the |
| 1022 | * matching server in the list. If the server is found, it is assigned to the |
| 1023 | * session. This always returns 1, and the analyser removes itself from the |
| 1024 | * list. Nothing is performed if a server was already assigned. |
| 1025 | */ |
| 1026 | int tcp_persist_rdp_cookie(struct session *s, struct buffer *req, int an_bit) |
| 1027 | { |
| 1028 | struct proxy *px = s->be; |
| 1029 | int ret; |
| 1030 | struct acl_expr expr; |
| 1031 | struct acl_test test; |
| 1032 | struct server *srv = px->srv; |
| 1033 | struct sockaddr_in addr; |
| 1034 | char *p; |
| 1035 | |
| 1036 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 1037 | now_ms, __FUNCTION__, |
| 1038 | s, |
| 1039 | req, |
| 1040 | req->rex, req->wex, |
| 1041 | req->flags, |
| 1042 | req->l, |
| 1043 | req->analysers); |
| 1044 | |
| 1045 | if (s->flags & SN_ASSIGNED) |
| 1046 | goto no_cookie; |
| 1047 | |
| 1048 | memset(&expr, 0, sizeof(expr)); |
| 1049 | memset(&test, 0, sizeof(test)); |
| 1050 | |
| 1051 | expr.arg.str = s->be->rdp_cookie_name; |
| 1052 | expr.arg_len = s->be->rdp_cookie_len; |
| 1053 | |
| 1054 | ret = acl_fetch_rdp_cookie(px, s, NULL, ACL_DIR_REQ, &expr, &test); |
| 1055 | if (ret == 0 || (test.flags & ACL_TEST_F_MAY_CHANGE) || test.len == 0) |
| 1056 | goto no_cookie; |
| 1057 | |
| 1058 | memset(&addr, 0, sizeof(addr)); |
| 1059 | addr.sin_family = AF_INET; |
| 1060 | |
| 1061 | /* Considering an rdp cookie detected using acl, test.ptr ended with <cr><lf> and should return */ |
| 1062 | addr.sin_addr.s_addr = strtoul(test.ptr, &p, 10); |
| 1063 | if (*p != '.') |
| 1064 | goto no_cookie; |
| 1065 | p++; |
| 1066 | addr.sin_port = (unsigned short)strtoul(p, &p, 10); |
| 1067 | if (*p != '.') |
| 1068 | goto no_cookie; |
| 1069 | |
| 1070 | while (srv) { |
| 1071 | if (memcmp(&addr, &(srv->addr), sizeof(addr)) == 0) { |
| 1072 | if ((srv->state & SRV_RUNNING) || (px->options & PR_O_PERSIST)) { |
| 1073 | /* we found the server and it is usable */ |
| 1074 | s->flags |= SN_DIRECT | SN_ASSIGNED; |
| 1075 | s->srv = srv; |
| 1076 | break; |
| 1077 | } |
| 1078 | } |
| 1079 | srv = srv->next; |
| 1080 | } |
| 1081 | |
| 1082 | no_cookie: |
| 1083 | req->analysers &= ~an_bit; |
| 1084 | req->analyse_exp = TICK_ETERNITY; |
| 1085 | return 1; |
| 1086 | } |
| 1087 | |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 1088 | int be_downtime(struct proxy *px) { |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 1089 | 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] | 1090 | return px->down_time; |
| 1091 | |
| 1092 | return now.tv_sec - px->last_change + px->down_time; |
| 1093 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1094 | |
Krzysztof Piotr Oledzki | 15514c2 | 2010-01-04 16:03:09 +0100 | [diff] [blame] | 1095 | /* |
| 1096 | * This function returns a string containing the balancing |
| 1097 | * mode of the proxy in a format suitable for stats. |
| 1098 | */ |
| 1099 | |
| 1100 | const char *backend_lb_algo_str(int algo) { |
| 1101 | |
| 1102 | if (algo == BE_LB_ALGO_RR) |
| 1103 | return "roundrobin"; |
| 1104 | else if (algo == BE_LB_ALGO_SRR) |
| 1105 | return "static-rr"; |
| 1106 | else if (algo == BE_LB_ALGO_LC) |
| 1107 | return "leastconn"; |
| 1108 | else if (algo == BE_LB_ALGO_SH) |
| 1109 | return "source"; |
| 1110 | else if (algo == BE_LB_ALGO_UH) |
| 1111 | return "uri"; |
| 1112 | else if (algo == BE_LB_ALGO_PH) |
| 1113 | return "url_param"; |
| 1114 | else if (algo == BE_LB_ALGO_HH) |
| 1115 | return "hdr"; |
| 1116 | else if (algo == BE_LB_ALGO_RCH) |
| 1117 | return "rdp-cookie"; |
| 1118 | else |
| 1119 | return NULL; |
| 1120 | } |
| 1121 | |
Willy Tarreau | a0cbda6 | 2007-11-01 21:39:54 +0100 | [diff] [blame] | 1122 | /* This function parses a "balance" statement in a backend section describing |
| 1123 | * <curproxy>. It returns -1 if there is any error, otherwise zero. If it |
| 1124 | * returns -1, it may write an error message into ther <err> buffer, for at |
| 1125 | * most <errlen> bytes, trailing zero included. The trailing '\n' will not be |
| 1126 | * written. The function must be called with <args> pointing to the first word |
| 1127 | * after "balance". |
| 1128 | */ |
| 1129 | int backend_parse_balance(const char **args, char *err, int errlen, struct proxy *curproxy) |
| 1130 | { |
| 1131 | if (!*(args[0])) { |
| 1132 | /* if no option is set, use round-robin by default */ |
Willy Tarreau | 3168223 | 2007-11-29 15:38:04 +0100 | [diff] [blame] | 1133 | curproxy->lbprm.algo &= ~BE_LB_ALGO; |
| 1134 | curproxy->lbprm.algo |= BE_LB_ALGO_RR; |
Willy Tarreau | a0cbda6 | 2007-11-01 21:39:54 +0100 | [diff] [blame] | 1135 | return 0; |
| 1136 | } |
| 1137 | |
| 1138 | if (!strcmp(args[0], "roundrobin")) { |
Willy Tarreau | 3168223 | 2007-11-29 15:38:04 +0100 | [diff] [blame] | 1139 | curproxy->lbprm.algo &= ~BE_LB_ALGO; |
| 1140 | curproxy->lbprm.algo |= BE_LB_ALGO_RR; |
Willy Tarreau | a0cbda6 | 2007-11-01 21:39:54 +0100 | [diff] [blame] | 1141 | } |
Willy Tarreau | 9757a38 | 2009-10-03 12:56:50 +0200 | [diff] [blame] | 1142 | else if (!strcmp(args[0], "static-rr")) { |
| 1143 | curproxy->lbprm.algo &= ~BE_LB_ALGO; |
| 1144 | curproxy->lbprm.algo |= BE_LB_ALGO_SRR; |
| 1145 | } |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 1146 | else if (!strcmp(args[0], "leastconn")) { |
| 1147 | curproxy->lbprm.algo &= ~BE_LB_ALGO; |
| 1148 | curproxy->lbprm.algo |= BE_LB_ALGO_LC; |
| 1149 | } |
Willy Tarreau | a0cbda6 | 2007-11-01 21:39:54 +0100 | [diff] [blame] | 1150 | else if (!strcmp(args[0], "source")) { |
Willy Tarreau | 3168223 | 2007-11-29 15:38:04 +0100 | [diff] [blame] | 1151 | curproxy->lbprm.algo &= ~BE_LB_ALGO; |
| 1152 | curproxy->lbprm.algo |= BE_LB_ALGO_SH; |
Willy Tarreau | a0cbda6 | 2007-11-01 21:39:54 +0100 | [diff] [blame] | 1153 | } |
| 1154 | else if (!strcmp(args[0], "uri")) { |
Marek Majkowski | 9c30fc1 | 2008-04-27 23:25:55 +0200 | [diff] [blame] | 1155 | int arg = 1; |
| 1156 | |
Willy Tarreau | 3168223 | 2007-11-29 15:38:04 +0100 | [diff] [blame] | 1157 | curproxy->lbprm.algo &= ~BE_LB_ALGO; |
| 1158 | curproxy->lbprm.algo |= BE_LB_ALGO_UH; |
Marek Majkowski | 9c30fc1 | 2008-04-27 23:25:55 +0200 | [diff] [blame] | 1159 | |
| 1160 | while (*args[arg]) { |
| 1161 | if (!strcmp(args[arg], "len")) { |
| 1162 | if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) { |
| 1163 | snprintf(err, errlen, "'balance uri len' expects a positive integer (got '%s').", args[arg+1]); |
| 1164 | return -1; |
| 1165 | } |
| 1166 | curproxy->uri_len_limit = atoi(args[arg+1]); |
| 1167 | arg += 2; |
| 1168 | } |
| 1169 | else if (!strcmp(args[arg], "depth")) { |
| 1170 | if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) { |
| 1171 | snprintf(err, errlen, "'balance uri depth' expects a positive integer (got '%s').", args[arg+1]); |
| 1172 | return -1; |
| 1173 | } |
| 1174 | /* hint: we store the position of the ending '/' (depth+1) so |
| 1175 | * that we avoid a comparison while computing the hash. |
| 1176 | */ |
| 1177 | curproxy->uri_dirs_depth1 = atoi(args[arg+1]) + 1; |
| 1178 | arg += 2; |
| 1179 | } |
| 1180 | else { |
| 1181 | snprintf(err, errlen, "'balance uri' only accepts parameters 'len' and 'depth' (got '%s').", args[arg]); |
| 1182 | return -1; |
| 1183 | } |
| 1184 | } |
Willy Tarreau | a0cbda6 | 2007-11-01 21:39:54 +0100 | [diff] [blame] | 1185 | } |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 1186 | else if (!strcmp(args[0], "url_param")) { |
| 1187 | if (!*args[1]) { |
| 1188 | snprintf(err, errlen, "'balance url_param' requires an URL parameter name."); |
| 1189 | return -1; |
| 1190 | } |
Willy Tarreau | 3168223 | 2007-11-29 15:38:04 +0100 | [diff] [blame] | 1191 | curproxy->lbprm.algo &= ~BE_LB_ALGO; |
| 1192 | curproxy->lbprm.algo |= BE_LB_ALGO_PH; |
Willy Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 1193 | |
| 1194 | free(curproxy->url_param_name); |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 1195 | curproxy->url_param_name = strdup(args[1]); |
Willy Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 1196 | curproxy->url_param_len = strlen(args[1]); |
Marek Majkowski | 9c30fc1 | 2008-04-27 23:25:55 +0200 | [diff] [blame] | 1197 | if (*args[2]) { |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 1198 | if (strcmp(args[2], "check_post")) { |
| 1199 | snprintf(err, errlen, "'balance url_param' only accepts check_post modifier."); |
| 1200 | return -1; |
| 1201 | } |
| 1202 | if (*args[3]) { |
| 1203 | /* TODO: maybe issue a warning if there is no value, no digits or too long */ |
| 1204 | curproxy->url_param_post_limit = str2ui(args[3]); |
| 1205 | } |
| 1206 | /* if no limit, or faul value in args[3], then default to a moderate wordlen */ |
| 1207 | if (!curproxy->url_param_post_limit) |
| 1208 | curproxy->url_param_post_limit = 48; |
| 1209 | else if ( curproxy->url_param_post_limit < 3 ) |
| 1210 | curproxy->url_param_post_limit = 3; /* minimum example: S=3 or \r\nS=6& */ |
| 1211 | } |
Benoit | affb481 | 2009-03-25 13:02:10 +0100 | [diff] [blame] | 1212 | } |
| 1213 | else if (!strncmp(args[0], "hdr(", 4)) { |
| 1214 | const char *beg, *end; |
| 1215 | |
| 1216 | beg = args[0] + 4; |
| 1217 | end = strchr(beg, ')'); |
| 1218 | |
| 1219 | if (!end || end == beg) { |
| 1220 | snprintf(err, errlen, "'balance hdr(name)' requires an http header field name."); |
| 1221 | return -1; |
| 1222 | } |
| 1223 | |
| 1224 | curproxy->lbprm.algo &= ~BE_LB_ALGO; |
| 1225 | curproxy->lbprm.algo |= BE_LB_ALGO_HH; |
| 1226 | |
| 1227 | free(curproxy->hh_name); |
| 1228 | curproxy->hh_len = end - beg; |
| 1229 | curproxy->hh_name = my_strndup(beg, end - beg); |
| 1230 | curproxy->hh_match_domain = 0; |
| 1231 | |
| 1232 | if (*args[1]) { |
| 1233 | if (strcmp(args[1], "use_domain_only")) { |
| 1234 | snprintf(err, errlen, "'balance hdr(name)' only accepts 'use_domain_only' modifier."); |
| 1235 | return -1; |
| 1236 | } |
| 1237 | curproxy->hh_match_domain = 1; |
| 1238 | } |
| 1239 | |
Emeric Brun | 736aa23 | 2009-06-30 17:56:00 +0200 | [diff] [blame] | 1240 | } |
| 1241 | else if (!strncmp(args[0], "rdp-cookie", 10)) { |
| 1242 | curproxy->lbprm.algo &= ~BE_LB_ALGO; |
| 1243 | curproxy->lbprm.algo |= BE_LB_ALGO_RCH; |
| 1244 | |
| 1245 | if ( *(args[0] + 10 ) == '(' ) { /* cookie name */ |
| 1246 | const char *beg, *end; |
| 1247 | |
| 1248 | beg = args[0] + 11; |
| 1249 | end = strchr(beg, ')'); |
| 1250 | |
| 1251 | if (!end || end == beg) { |
| 1252 | snprintf(err, errlen, "'balance rdp-cookie(name)' requires an rdp cookie name."); |
| 1253 | return -1; |
| 1254 | } |
| 1255 | |
| 1256 | free(curproxy->hh_name); |
| 1257 | curproxy->hh_name = my_strndup(beg, end - beg); |
| 1258 | curproxy->hh_len = end - beg; |
| 1259 | } |
| 1260 | else if ( *(args[0] + 10 ) == '\0' ) { /* default cookie name 'mstshash' */ |
| 1261 | free(curproxy->hh_name); |
| 1262 | curproxy->hh_name = strdup("mstshash"); |
| 1263 | curproxy->hh_len = strlen(curproxy->hh_name); |
| 1264 | } |
| 1265 | else { /* syntax */ |
| 1266 | snprintf(err, errlen, "'balance rdp-cookie(name)' requires an rdp cookie name."); |
| 1267 | return -1; |
| 1268 | } |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 1269 | } |
Willy Tarreau | a0cbda6 | 2007-11-01 21:39:54 +0100 | [diff] [blame] | 1270 | else { |
Willy Tarreau | 9757a38 | 2009-10-03 12:56:50 +0200 | [diff] [blame] | 1271 | 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] | 1272 | return -1; |
| 1273 | } |
| 1274 | return 0; |
| 1275 | } |
| 1276 | |
Willy Tarreau | a9d3c1e | 2007-11-30 20:48:53 +0100 | [diff] [blame] | 1277 | |
| 1278 | /************************************************************************/ |
| 1279 | /* All supported keywords must be declared here. */ |
| 1280 | /************************************************************************/ |
| 1281 | |
| 1282 | /* set test->i to the number of enabled servers on the proxy */ |
| 1283 | static int |
| 1284 | acl_fetch_nbsrv(struct proxy *px, struct session *l4, void *l7, int dir, |
| 1285 | struct acl_expr *expr, struct acl_test *test) |
| 1286 | { |
| 1287 | test->flags = ACL_TEST_F_VOL_TEST; |
| 1288 | if (expr->arg_len) { |
| 1289 | /* another proxy was designated, we must look for it */ |
| 1290 | for (px = proxy; px; px = px->next) |
| 1291 | if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str)) |
| 1292 | break; |
| 1293 | } |
| 1294 | if (!px) |
| 1295 | return 0; |
| 1296 | |
| 1297 | if (px->srv_act) |
| 1298 | test->i = px->srv_act; |
| 1299 | else if (px->lbprm.fbck) |
| 1300 | test->i = 1; |
| 1301 | else |
| 1302 | test->i = px->srv_bck; |
| 1303 | |
| 1304 | return 1; |
| 1305 | } |
| 1306 | |
Willy Tarreau | 0b1cd94 | 2010-05-16 22:18:27 +0200 | [diff] [blame] | 1307 | /* report in test->flags a success or failure depending on the designated |
| 1308 | * server's state. There is no match function involved since there's no pattern. |
| 1309 | */ |
| 1310 | static int |
| 1311 | acl_fetch_srv_is_up(struct proxy *px, struct session *l4, void *l7, int dir, |
| 1312 | struct acl_expr *expr, struct acl_test *test) |
| 1313 | { |
| 1314 | struct server *srv = expr->arg.srv; |
| 1315 | |
| 1316 | test->flags = ACL_TEST_F_VOL_TEST; |
| 1317 | if (!(srv->state & SRV_MAINTAIN) && |
| 1318 | (!(srv->state & SRV_CHECKED) || (srv->state & SRV_RUNNING))) |
| 1319 | test->flags |= ACL_TEST_F_SET_RES_PASS; |
| 1320 | else |
| 1321 | test->flags |= ACL_TEST_F_SET_RES_FAIL; |
| 1322 | return 1; |
| 1323 | } |
| 1324 | |
Jeffrey 'jf' Lim | 5051d7b | 2008-09-04 01:03:03 +0800 | [diff] [blame] | 1325 | /* set test->i to the number of enabled servers on the proxy */ |
| 1326 | static int |
| 1327 | acl_fetch_connslots(struct proxy *px, struct session *l4, void *l7, int dir, |
| 1328 | struct acl_expr *expr, struct acl_test *test) |
| 1329 | { |
| 1330 | struct server *iterator; |
| 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 | test->i = 0; |
| 1342 | iterator = px->srv; |
| 1343 | while (iterator) { |
Willy Tarreau | a36af91 | 2009-10-10 12:02:45 +0200 | [diff] [blame] | 1344 | if ((iterator->state & SRV_RUNNING) == 0) { |
Jeffrey 'jf' Lim | 5051d7b | 2008-09-04 01:03:03 +0800 | [diff] [blame] | 1345 | iterator = iterator->next; |
| 1346 | continue; |
| 1347 | } |
| 1348 | if (iterator->maxconn == 0 || iterator->maxqueue == 0) { |
| 1349 | test->i = -1; |
| 1350 | return 1; |
| 1351 | } |
| 1352 | |
| 1353 | test->i += (iterator->maxconn - iterator->cur_sess) |
| 1354 | + (iterator->maxqueue - iterator->nbpend); |
| 1355 | iterator = iterator->next; |
| 1356 | } |
| 1357 | |
| 1358 | return 1; |
| 1359 | } |
| 1360 | |
Willy Tarreau | 079ff0a | 2009-03-05 21:34:28 +0100 | [diff] [blame] | 1361 | /* set test->i to the number of connections per second reaching the backend */ |
| 1362 | static int |
| 1363 | acl_fetch_be_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir, |
| 1364 | struct acl_expr *expr, struct acl_test *test) |
| 1365 | { |
| 1366 | test->flags = ACL_TEST_F_VOL_TEST; |
| 1367 | if (expr->arg_len) { |
| 1368 | /* another proxy was designated, we must look for it */ |
| 1369 | for (px = proxy; px; px = px->next) |
| 1370 | if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str)) |
| 1371 | break; |
| 1372 | } |
| 1373 | if (!px) |
| 1374 | return 0; |
| 1375 | |
| 1376 | test->i = read_freq_ctr(&px->be_sess_per_sec); |
| 1377 | return 1; |
| 1378 | } |
| 1379 | |
Willy Tarreau | a36af91 | 2009-10-10 12:02:45 +0200 | [diff] [blame] | 1380 | /* set test->i to the number of concurrent connections on the backend */ |
| 1381 | static int |
| 1382 | acl_fetch_be_conn(struct proxy *px, struct session *l4, void *l7, int dir, |
| 1383 | struct acl_expr *expr, struct acl_test *test) |
| 1384 | { |
| 1385 | test->flags = ACL_TEST_F_VOL_TEST; |
| 1386 | if (expr->arg_len) { |
| 1387 | /* another proxy was designated, we must look for it */ |
| 1388 | for (px = proxy; px; px = px->next) |
| 1389 | if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str)) |
| 1390 | break; |
| 1391 | } |
| 1392 | if (!px) |
| 1393 | return 0; |
| 1394 | |
| 1395 | test->i = px->beconn; |
| 1396 | return 1; |
| 1397 | } |
| 1398 | |
| 1399 | /* set test->i to the total number of queued connections on the backend */ |
| 1400 | static int |
| 1401 | acl_fetch_queue_size(struct proxy *px, struct session *l4, void *l7, int dir, |
| 1402 | struct acl_expr *expr, struct acl_test *test) |
| 1403 | { |
| 1404 | test->flags = ACL_TEST_F_VOL_TEST; |
| 1405 | if (expr->arg_len) { |
| 1406 | /* another proxy was designated, we must look for it */ |
| 1407 | for (px = proxy; px; px = px->next) |
| 1408 | if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str)) |
| 1409 | break; |
| 1410 | } |
| 1411 | if (!px) |
| 1412 | return 0; |
| 1413 | |
| 1414 | test->i = px->totpend; |
| 1415 | return 1; |
| 1416 | } |
| 1417 | |
| 1418 | /* set test->i to the total number of queued connections on the backend divided |
| 1419 | * by the number of running servers and rounded up. If there is no running |
| 1420 | * server, we return twice the total, just as if we had half a running server. |
| 1421 | * This is more or less correct anyway, since we expect the last server to come |
| 1422 | * back soon. |
| 1423 | */ |
| 1424 | static int |
| 1425 | acl_fetch_avg_queue_size(struct proxy *px, struct session *l4, void *l7, int dir, |
| 1426 | struct acl_expr *expr, struct acl_test *test) |
| 1427 | { |
| 1428 | int nbsrv; |
| 1429 | |
| 1430 | test->flags = ACL_TEST_F_VOL_TEST; |
| 1431 | if (expr->arg_len) { |
| 1432 | /* another proxy was designated, we must look for it */ |
| 1433 | for (px = proxy; px; px = px->next) |
| 1434 | if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str)) |
| 1435 | break; |
| 1436 | } |
| 1437 | if (!px) |
| 1438 | return 0; |
| 1439 | |
| 1440 | if (px->srv_act) |
| 1441 | nbsrv = px->srv_act; |
| 1442 | else if (px->lbprm.fbck) |
| 1443 | nbsrv = 1; |
| 1444 | else |
| 1445 | nbsrv = px->srv_bck; |
| 1446 | |
| 1447 | if (nbsrv > 0) |
| 1448 | test->i = (px->totpend + nbsrv - 1) / nbsrv; |
| 1449 | else |
| 1450 | test->i = px->totpend * 2; |
| 1451 | |
| 1452 | return 1; |
| 1453 | } |
Willy Tarreau | a9d3c1e | 2007-11-30 20:48:53 +0100 | [diff] [blame] | 1454 | |
| 1455 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 1456 | static struct acl_kw_list acl_kws = {{ },{ |
Jeffrey 'jf' Lim | 5051d7b | 2008-09-04 01:03:03 +0800 | [diff] [blame] | 1457 | { "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] | 1458 | { "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] | 1459 | { "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] | 1460 | { "be_conn", acl_parse_int, acl_fetch_be_conn, acl_match_int, ACL_USE_NOTHING }, |
| 1461 | { "queue", acl_parse_int, acl_fetch_queue_size, acl_match_int, ACL_USE_NOTHING }, |
| 1462 | { "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] | 1463 | { "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] | 1464 | { NULL, NULL, NULL, NULL }, |
| 1465 | }}; |
| 1466 | |
| 1467 | |
| 1468 | __attribute__((constructor)) |
| 1469 | static void __backend_init(void) |
| 1470 | { |
| 1471 | acl_register_keywords(&acl_kws); |
| 1472 | } |
| 1473 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1474 | /* |
| 1475 | * Local variables: |
| 1476 | * c-indent-level: 8 |
| 1477 | * c-basic-offset: 8 |
| 1478 | * End: |
| 1479 | */ |