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