Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Backend variables and functions. |
| 3 | * |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 4 | * Copyright 2000-2009 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 | 14c8aac | 2007-05-08 19:46:30 +0200 | [diff] [blame] | 32 | #include <proto/client.h> |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 33 | #include <proto/lb_fwlc.h> |
| 34 | #include <proto/lb_fwrr.h> |
| 35 | #include <proto/lb_map.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 36 | #include <proto/proto_http.h> |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 37 | #include <proto/proto_tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 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: |
| 120 | return map_get_server_hash(px, h); |
| 121 | } |
| 122 | |
| 123 | /* |
| 124 | * This function tries to find a running server for the proxy <px> following |
| 125 | * the URI hash method. In order to optimize cache hits, the hash computation |
| 126 | * ends at the question mark. Depending on the number of active/backup servers, |
| 127 | * it will either look for active servers, or for backup servers. |
| 128 | * If any server is found, it will be returned. If no valid server is found, |
| 129 | * NULL is returned. |
| 130 | * |
| 131 | * This code was contributed by Guillaume Dallaire, who also selected this hash |
| 132 | * algorithm out of a tens because it gave him the best results. |
| 133 | * |
| 134 | */ |
| 135 | struct server *get_server_uh(struct proxy *px, char *uri, int uri_len) |
| 136 | { |
| 137 | unsigned long hash = 0; |
| 138 | int c; |
| 139 | int slashes = 0; |
| 140 | |
| 141 | if (px->lbprm.tot_weight == 0) |
| 142 | return NULL; |
| 143 | |
| 144 | /* note: we won't hash if there's only one server left */ |
| 145 | if (px->lbprm.tot_used == 1) |
| 146 | goto hash_done; |
| 147 | |
| 148 | if (px->uri_len_limit) |
| 149 | uri_len = MIN(uri_len, px->uri_len_limit); |
| 150 | |
| 151 | while (uri_len--) { |
| 152 | c = *uri++; |
| 153 | if (c == '/') { |
| 154 | slashes++; |
| 155 | if (slashes == px->uri_dirs_depth1) /* depth+1 */ |
| 156 | break; |
| 157 | } |
| 158 | else if (c == '?') |
| 159 | break; |
| 160 | |
| 161 | hash = c + (hash << 6) + (hash << 16) - hash; |
| 162 | } |
| 163 | hash_done: |
| 164 | return map_get_server_hash(px, hash); |
| 165 | } |
| 166 | |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 167 | /* |
| 168 | * This function tries to find a running server for the proxy <px> following |
| 169 | * the URL parameter hash method. It looks for a specific parameter in the |
| 170 | * URL and hashes it to compute the server ID. This is useful to optimize |
| 171 | * performance by avoiding bounces between servers in contexts where sessions |
| 172 | * are shared but cookies are not usable. If the parameter is not found, NULL |
| 173 | * is returned. If any server is found, it will be returned. If no valid server |
| 174 | * is found, NULL is returned. |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 175 | */ |
| 176 | struct server *get_server_ph(struct proxy *px, const char *uri, int uri_len) |
| 177 | { |
| 178 | unsigned long hash = 0; |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 179 | const char *p; |
| 180 | const char *params; |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 181 | int plen; |
| 182 | |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 183 | /* when tot_weight is 0 then so is srv_count */ |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 184 | if (px->lbprm.tot_weight == 0) |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 185 | return NULL; |
| 186 | |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 187 | if ((p = memchr(uri, '?', uri_len)) == NULL) |
| 188 | return NULL; |
| 189 | |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 190 | p++; |
| 191 | |
| 192 | uri_len -= (p - uri); |
| 193 | plen = px->url_param_len; |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 194 | params = p; |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 195 | |
| 196 | while (uri_len > plen) { |
| 197 | /* Look for the parameter name followed by an equal symbol */ |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 198 | if (params[plen] == '=') { |
| 199 | if (memcmp(params, px->url_param_name, plen) == 0) { |
| 200 | /* OK, we have the parameter here at <params>, and |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 201 | * the value after the equal sign, at <p> |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 202 | * skip the equal symbol |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 203 | */ |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 204 | p += plen + 1; |
| 205 | uri_len -= plen + 1; |
| 206 | |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 207 | while (uri_len && *p != '&') { |
| 208 | hash = *p + (hash << 6) + (hash << 16) - hash; |
| 209 | uri_len--; |
| 210 | p++; |
| 211 | } |
Willy Tarreau | 39c9ba7 | 2009-10-01 21:11:15 +0200 | [diff] [blame] | 212 | return map_get_server_hash(px, hash); |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 213 | } |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 214 | } |
| 215 | /* skip to next parameter */ |
| 216 | p = memchr(params, '&', uri_len); |
| 217 | if (!p) |
| 218 | return NULL; |
| 219 | p++; |
| 220 | uri_len -= (p - params); |
| 221 | params = p; |
| 222 | } |
| 223 | return NULL; |
| 224 | } |
| 225 | |
| 226 | /* |
| 227 | * this does the same as the previous server_ph, but check the body contents |
| 228 | */ |
| 229 | struct server *get_server_ph_post(struct session *s) |
| 230 | { |
| 231 | unsigned long hash = 0; |
| 232 | struct http_txn *txn = &s->txn; |
| 233 | struct buffer *req = s->req; |
| 234 | struct http_msg *msg = &txn->req; |
| 235 | struct proxy *px = s->be; |
| 236 | unsigned int plen = px->url_param_len; |
Willy Tarreau | 192ee3e | 2008-04-19 21:24:56 +0200 | [diff] [blame] | 237 | unsigned long body; |
| 238 | unsigned long len; |
| 239 | const char *params; |
| 240 | struct hdr_ctx ctx; |
| 241 | const char *p; |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 242 | |
| 243 | /* tot_weight appears to mean srv_count */ |
| 244 | if (px->lbprm.tot_weight == 0) |
| 245 | return NULL; |
| 246 | |
Willy Tarreau | 192ee3e | 2008-04-19 21:24:56 +0200 | [diff] [blame] | 247 | body = msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1; |
Willy Tarreau | fb0528b | 2008-08-11 00:21:56 +0200 | [diff] [blame] | 248 | len = req->l - body; |
Willy Tarreau | 192ee3e | 2008-04-19 21:24:56 +0200 | [diff] [blame] | 249 | params = req->data + body; |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 250 | |
| 251 | if ( len == 0 ) |
| 252 | return NULL; |
| 253 | |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 254 | ctx.idx = 0; |
| 255 | |
| 256 | /* if the message is chunked, we skip the chunk size, but use the value as len */ |
| 257 | http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 258 | if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) { |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 259 | unsigned int chunk = 0; |
Willy Tarreau | 03d60bb | 2009-01-09 11:13:00 +0100 | [diff] [blame] | 260 | while ( params < (req->data+req->max_len) && !HTTP_IS_CRLF(*params)) { |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 261 | char c = *params; |
| 262 | if (ishex(c)) { |
| 263 | unsigned int hex = toupper(c) - '0'; |
| 264 | if ( hex > 9 ) |
| 265 | hex -= 'A' - '9' - 1; |
| 266 | chunk = (chunk << 4) | hex; |
| 267 | } |
| 268 | else |
| 269 | return NULL; |
| 270 | params++; |
| 271 | len--; |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 272 | } |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 273 | /* spec says we get CRLF */ |
| 274 | if (HTTP_IS_CRLF(*params) && HTTP_IS_CRLF(params[1])) |
| 275 | params += 2; |
| 276 | else |
| 277 | return NULL; |
| 278 | /* ok we have some encoded length, just inspect the first chunk */ |
| 279 | len = chunk; |
| 280 | } |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 281 | |
Willy Tarreau | 192ee3e | 2008-04-19 21:24:56 +0200 | [diff] [blame] | 282 | p = params; |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 283 | |
| 284 | while (len > plen) { |
| 285 | /* Look for the parameter name followed by an equal symbol */ |
| 286 | if (params[plen] == '=') { |
| 287 | if (memcmp(params, px->url_param_name, plen) == 0) { |
| 288 | /* OK, we have the parameter here at <params>, and |
| 289 | * the value after the equal sign, at <p> |
| 290 | * skip the equal symbol |
| 291 | */ |
| 292 | p += plen + 1; |
| 293 | len -= plen + 1; |
| 294 | |
| 295 | while (len && *p != '&') { |
| 296 | if (unlikely(!HTTP_IS_TOKEN(*p))) { |
| 297 | /* if in a POST, body must be URI encoded or its not a URI. |
| 298 | * Do not interprete any possible binary data as a parameter. |
| 299 | */ |
| 300 | if (likely(HTTP_IS_LWS(*p))) /* eol, uncertain uri len */ |
| 301 | break; |
| 302 | return NULL; /* oh, no; this is not uri-encoded. |
| 303 | * This body does not contain parameters. |
| 304 | */ |
| 305 | } |
| 306 | hash = *p + (hash << 6) + (hash << 16) - hash; |
| 307 | len--; |
| 308 | p++; |
| 309 | /* should we break if vlen exceeds limit? */ |
| 310 | } |
Willy Tarreau | 39c9ba7 | 2009-10-01 21:11:15 +0200 | [diff] [blame] | 311 | return map_get_server_hash(px, hash); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 312 | } |
| 313 | } |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 314 | /* skip to next parameter */ |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 315 | p = memchr(params, '&', len); |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 316 | if (!p) |
| 317 | return NULL; |
| 318 | p++; |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 319 | len -= (p - params); |
| 320 | params = p; |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 321 | } |
| 322 | return NULL; |
| 323 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 324 | |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 325 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 326 | /* |
Benoit | affb481 | 2009-03-25 13:02:10 +0100 | [diff] [blame] | 327 | * This function tries to find a running server for the proxy <px> following |
| 328 | * the Header parameter hash method. It looks for a specific parameter in the |
| 329 | * URL and hashes it to compute the server ID. This is useful to optimize |
| 330 | * performance by avoiding bounces between servers in contexts where sessions |
| 331 | * are shared but cookies are not usable. If the parameter is not found, NULL |
| 332 | * is returned. If any server is found, it will be returned. If no valid server |
| 333 | * is found, NULL is returned. |
| 334 | */ |
| 335 | struct server *get_server_hh(struct session *s) |
| 336 | { |
| 337 | unsigned long hash = 0; |
| 338 | struct http_txn *txn = &s->txn; |
| 339 | struct http_msg *msg = &txn->req; |
| 340 | struct proxy *px = s->be; |
| 341 | unsigned int plen = px->hh_len; |
| 342 | unsigned long len; |
| 343 | struct hdr_ctx ctx; |
| 344 | const char *p; |
| 345 | |
| 346 | /* tot_weight appears to mean srv_count */ |
| 347 | if (px->lbprm.tot_weight == 0) |
| 348 | return NULL; |
| 349 | |
Benoit | affb481 | 2009-03-25 13:02:10 +0100 | [diff] [blame] | 350 | ctx.idx = 0; |
| 351 | |
| 352 | /* if the message is chunked, we skip the chunk size, but use the value as len */ |
| 353 | http_find_header2(px->hh_name, plen, msg->sol, &txn->hdr_idx, &ctx); |
| 354 | |
| 355 | /* if the header is not found or empty, let's fallback to round robin */ |
| 356 | if (!ctx.idx || !ctx.vlen) |
| 357 | return NULL; |
| 358 | |
Willy Tarreau | 39c9ba7 | 2009-10-01 21:11:15 +0200 | [diff] [blame] | 359 | /* note: we won't hash if there's only one server left */ |
| 360 | if (px->lbprm.tot_used == 1) |
| 361 | goto hash_done; |
| 362 | |
Benoit | affb481 | 2009-03-25 13:02:10 +0100 | [diff] [blame] | 363 | /* Found a the hh_name in the headers. |
| 364 | * we will compute the hash based on this value ctx.val. |
| 365 | */ |
| 366 | len = ctx.vlen; |
| 367 | p = (char *)ctx.line + ctx.val; |
| 368 | if (!px->hh_match_domain) { |
| 369 | while (len) { |
| 370 | hash = *p + (hash << 6) + (hash << 16) - hash; |
| 371 | len--; |
| 372 | p++; |
| 373 | } |
| 374 | } else { |
| 375 | int dohash = 0; |
| 376 | p += len - 1; |
| 377 | /* special computation, use only main domain name, not tld/host |
| 378 | * going back from the end of string, start hashing at first |
| 379 | * dot stop at next. |
| 380 | * This is designed to work with the 'Host' header, and requires |
| 381 | * a special option to activate this. |
| 382 | */ |
| 383 | while (len) { |
| 384 | if (*p == '.') { |
| 385 | if (!dohash) |
| 386 | dohash = 1; |
| 387 | else |
| 388 | break; |
| 389 | } else { |
| 390 | if (dohash) |
| 391 | hash = *p + (hash << 6) + (hash << 16) - hash; |
| 392 | } |
| 393 | len--; |
| 394 | p--; |
| 395 | } |
| 396 | } |
Willy Tarreau | 39c9ba7 | 2009-10-01 21:11:15 +0200 | [diff] [blame] | 397 | hash_done: |
| 398 | return map_get_server_hash(px, hash); |
Benoit | affb481 | 2009-03-25 13:02:10 +0100 | [diff] [blame] | 399 | } |
| 400 | |
Emeric Brun | 736aa23 | 2009-06-30 17:56:00 +0200 | [diff] [blame] | 401 | struct server *get_server_rch(struct session *s) |
| 402 | { |
| 403 | unsigned long hash = 0; |
| 404 | struct proxy *px = s->be; |
| 405 | unsigned long len; |
| 406 | const char *p; |
| 407 | int ret; |
| 408 | struct acl_expr expr; |
| 409 | struct acl_test test; |
| 410 | |
| 411 | /* tot_weight appears to mean srv_count */ |
| 412 | if (px->lbprm.tot_weight == 0) |
| 413 | return NULL; |
| 414 | |
Emeric Brun | 736aa23 | 2009-06-30 17:56:00 +0200 | [diff] [blame] | 415 | memset(&expr, 0, sizeof(expr)); |
| 416 | memset(&test, 0, sizeof(test)); |
| 417 | |
| 418 | expr.arg.str = px->hh_name; |
| 419 | expr.arg_len = px->hh_len; |
| 420 | |
| 421 | ret = acl_fetch_rdp_cookie(px, s, NULL, ACL_DIR_REQ, &expr, &test); |
| 422 | if (ret == 0 || (test.flags & ACL_TEST_F_MAY_CHANGE) || test.len == 0) |
| 423 | return NULL; |
| 424 | |
Willy Tarreau | 39c9ba7 | 2009-10-01 21:11:15 +0200 | [diff] [blame] | 425 | /* note: we won't hash if there's only one server left */ |
| 426 | if (px->lbprm.tot_used == 1) |
| 427 | goto hash_done; |
| 428 | |
Emeric Brun | 736aa23 | 2009-06-30 17:56:00 +0200 | [diff] [blame] | 429 | /* Found a the hh_name in the headers. |
| 430 | * we will compute the hash based on this value ctx.val. |
| 431 | */ |
| 432 | len = test.len; |
| 433 | p = (char *)test.ptr; |
| 434 | while (len) { |
| 435 | hash = *p + (hash << 6) + (hash << 16) - hash; |
| 436 | len--; |
| 437 | p++; |
| 438 | } |
Willy Tarreau | 39c9ba7 | 2009-10-01 21:11:15 +0200 | [diff] [blame] | 439 | hash_done: |
| 440 | return map_get_server_hash(px, hash); |
Emeric Brun | 736aa23 | 2009-06-30 17:56:00 +0200 | [diff] [blame] | 441 | } |
Benoit | affb481 | 2009-03-25 13:02:10 +0100 | [diff] [blame] | 442 | |
| 443 | /* |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 444 | * This function applies the load-balancing algorithm to the session, as |
| 445 | * defined by the backend it is assigned to. The session is then marked as |
| 446 | * 'assigned'. |
| 447 | * |
| 448 | * This function MAY NOT be called with SN_ASSIGNED already set. If the session |
| 449 | * had a server previously assigned, it is rebalanced, trying to avoid the same |
| 450 | * server. |
| 451 | * The function tries to keep the original connection slot if it reconnects to |
| 452 | * the same server, otherwise it releases it and tries to offer it. |
| 453 | * |
| 454 | * 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] | 455 | * |
| 456 | * It may return : |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 457 | * SRV_STATUS_OK if everything is OK. Session assigned to ->srv |
| 458 | * SRV_STATUS_NOSRV if no server is available. Session is not ASSIGNED |
| 459 | * SRV_STATUS_FULL if all servers are saturated. Session is not ASSIGNED |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 460 | * SRV_STATUS_INTERNAL for other unrecoverable errors. |
| 461 | * |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 462 | * Upon successful return, the session flag SN_ASSIGNED is set to indicate that |
| 463 | * it does not need to be called anymore. This means that s->srv can be trusted |
| 464 | * in balance and direct modes. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 465 | * |
| 466 | */ |
| 467 | |
| 468 | int assign_server(struct session *s) |
| 469 | { |
Krzysztof Piotr Oledzki | 5a329cf | 2008-02-22 03:50:19 +0100 | [diff] [blame] | 470 | |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 471 | struct server *conn_slot; |
| 472 | int err; |
Krzysztof Piotr Oledzki | 5a329cf | 2008-02-22 03:50:19 +0100 | [diff] [blame] | 473 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 474 | #ifdef DEBUG_FULL |
| 475 | fprintf(stderr,"assign_server : s=%p\n",s); |
| 476 | #endif |
| 477 | |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 478 | err = SRV_STATUS_INTERNAL; |
| 479 | if (unlikely(s->pend_pos || s->flags & SN_ASSIGNED)) |
| 480 | goto out_err; |
Krzysztof Piotr Oledzki | 5a329cf | 2008-02-22 03:50:19 +0100 | [diff] [blame] | 481 | |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 482 | s->prev_srv = s->prev_srv; |
| 483 | conn_slot = s->srv_conn; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 484 | |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 485 | /* We have to release any connection slot before applying any LB algo, |
| 486 | * otherwise we may erroneously end up with no available slot. |
| 487 | */ |
| 488 | if (conn_slot) |
| 489 | sess_change_server(s, NULL); |
| 490 | |
| 491 | /* We will now try to find the good server and store it into <s->srv>. |
| 492 | * Note that <s->srv> may be NULL in case of dispatch or proxy mode, |
| 493 | * as well as if no server is available (check error code). |
| 494 | */ |
Willy Tarreau | 1a20a5d | 2007-11-01 21:08:19 +0100 | [diff] [blame] | 495 | |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 496 | s->srv = NULL; |
Willy Tarreau | f3e49f9 | 2009-10-03 12:21:20 +0200 | [diff] [blame] | 497 | if (s->be->lbprm.algo & BE_LB_KIND) { |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 498 | int len; |
| 499 | /* we must check if we have at least one server available */ |
| 500 | if (!s->be->lbprm.tot_weight) { |
| 501 | err = SRV_STATUS_NOSRV; |
| 502 | goto out; |
| 503 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 504 | |
Willy Tarreau | da76f4f | 2009-10-03 12:36:05 +0200 | [diff] [blame] | 505 | /* First check whether we need to fetch some data or simply call |
| 506 | * the LB lookup function. Only the hashing functions will need |
| 507 | * some input data in fact, and will support multiple algorithms. |
| 508 | */ |
| 509 | switch (s->be->lbprm.algo & BE_LB_LKUP) { |
| 510 | case BE_LB_LKUP_RRTREE: |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 511 | s->srv = fwrr_get_next_server(s->be, s->prev_srv); |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 512 | break; |
Willy Tarreau | da76f4f | 2009-10-03 12:36:05 +0200 | [diff] [blame] | 513 | |
| 514 | case BE_LB_LKUP_LCTREE: |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 515 | s->srv = fwlc_get_next_server(s->be, s->prev_srv); |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 516 | break; |
Willy Tarreau | da76f4f | 2009-10-03 12:36:05 +0200 | [diff] [blame] | 517 | |
| 518 | case BE_LB_LKUP_MAP: |
Willy Tarreau | 9757a38 | 2009-10-03 12:56:50 +0200 | [diff] [blame] | 519 | if ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR) { |
| 520 | s->srv = map_get_server_rr(s->be, s->prev_srv); |
| 521 | break; |
| 522 | } |
| 523 | 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] | 524 | /* unknown balancing algorithm */ |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 525 | err = SRV_STATUS_INTERNAL; |
| 526 | goto out; |
| 527 | } |
Willy Tarreau | da76f4f | 2009-10-03 12:36:05 +0200 | [diff] [blame] | 528 | |
| 529 | switch (s->be->lbprm.algo & BE_LB_PARM) { |
| 530 | case BE_LB_HASH_SRC: |
| 531 | if (s->cli_addr.ss_family == AF_INET) |
| 532 | len = 4; |
| 533 | else if (s->cli_addr.ss_family == AF_INET6) |
| 534 | len = 16; |
| 535 | else { |
| 536 | /* unknown IP family */ |
| 537 | err = SRV_STATUS_INTERNAL; |
| 538 | goto out; |
| 539 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 540 | |
Willy Tarreau | da76f4f | 2009-10-03 12:36:05 +0200 | [diff] [blame] | 541 | s->srv = get_server_sh(s->be, |
| 542 | (void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 543 | len); |
| 544 | break; |
| 545 | |
| 546 | case BE_LB_HASH_URI: |
| 547 | /* URI hashing */ |
| 548 | s->srv = get_server_uh(s->be, |
Willy Tarreau | 2fcb500 | 2007-05-08 13:35:26 +0200 | [diff] [blame] | 549 | s->txn.req.sol + s->txn.req.sl.rq.u, |
| 550 | s->txn.req.sl.rq.u_l); |
Willy Tarreau | da76f4f | 2009-10-03 12:36:05 +0200 | [diff] [blame] | 551 | break; |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 552 | |
Willy Tarreau | da76f4f | 2009-10-03 12:36:05 +0200 | [diff] [blame] | 553 | case BE_LB_HASH_PRM: |
| 554 | /* URL Parameter hashing */ |
| 555 | if (s->txn.meth == HTTP_METH_POST && |
| 556 | memchr(s->txn.req.sol + s->txn.req.sl.rq.u, '&', |
| 557 | s->txn.req.sl.rq.u_l ) == NULL) |
| 558 | s->srv = get_server_ph_post(s); |
| 559 | else |
| 560 | s->srv = get_server_ph(s->be, |
| 561 | s->txn.req.sol + s->txn.req.sl.rq.u, |
| 562 | s->txn.req.sl.rq.u_l); |
| 563 | break; |
Benoit | affb481 | 2009-03-25 13:02:10 +0100 | [diff] [blame] | 564 | |
Willy Tarreau | da76f4f | 2009-10-03 12:36:05 +0200 | [diff] [blame] | 565 | case BE_LB_HASH_HDR: |
| 566 | /* Header Parameter hashing */ |
| 567 | s->srv = get_server_hh(s); |
| 568 | break; |
| 569 | |
| 570 | case BE_LB_HASH_RDP: |
| 571 | /* RDP Cookie hashing */ |
| 572 | s->srv = get_server_rch(s); |
| 573 | break; |
| 574 | |
| 575 | default: |
| 576 | /* unknown balancing algorithm */ |
| 577 | err = SRV_STATUS_INTERNAL; |
| 578 | goto out; |
Benoit | affb481 | 2009-03-25 13:02:10 +0100 | [diff] [blame] | 579 | } |
Emeric Brun | 736aa23 | 2009-06-30 17:56:00 +0200 | [diff] [blame] | 580 | |
Willy Tarreau | da76f4f | 2009-10-03 12:36:05 +0200 | [diff] [blame] | 581 | /* If the hashing parameter was not found, let's fall |
| 582 | * back to round robin on the map. |
| 583 | */ |
| 584 | if (!s->srv) |
Willy Tarreau | f89c187 | 2009-10-01 11:19:37 +0200 | [diff] [blame] | 585 | s->srv = map_get_server_rr(s->be, s->prev_srv); |
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 | |
| 824 | /* |
| 825 | * This function initiates a connection to the server assigned to this session |
| 826 | * (s->srv, s->srv_addr). It will assign a server if none is assigned yet. |
| 827 | * It can return one of : |
| 828 | * - SN_ERR_NONE if everything's OK |
| 829 | * - SN_ERR_SRVTO if there are no more servers |
| 830 | * - SN_ERR_SRVCL if the connection was refused by the server |
| 831 | * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn) |
| 832 | * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...) |
| 833 | * - SN_ERR_INTERNAL for any other purely internal errors |
| 834 | * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted. |
| 835 | */ |
| 836 | int connect_server(struct session *s) |
| 837 | { |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 838 | int err; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 839 | |
| 840 | if (!(s->flags & SN_ADDR_SET)) { |
| 841 | err = assign_server_address(s); |
| 842 | if (err != SRV_STATUS_OK) |
| 843 | return SN_ERR_INTERNAL; |
| 844 | } |
| 845 | |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 846 | if (!s->req->cons->connect) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 847 | return SN_ERR_INTERNAL; |
Willy Tarreau | d88edf2 | 2009-06-14 15:48:17 +0200 | [diff] [blame] | 848 | |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 849 | err = s->req->cons->connect(s->req->cons, s->be, s->srv, |
| 850 | (struct sockaddr *)&s->srv_addr, |
| 851 | (struct sockaddr *)&s->cli_addr); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 852 | |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 853 | if (err != SN_ERR_NONE) |
| 854 | return err; |
Willy Tarreau | 788e284 | 2008-08-26 13:25:39 +0200 | [diff] [blame] | 855 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 856 | if (s->srv) { |
Willy Tarreau | 1e62de6 | 2008-11-11 20:20:02 +0100 | [diff] [blame] | 857 | s->flags |= SN_CURR_SESS; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 858 | s->srv->cur_sess++; |
| 859 | if (s->srv->cur_sess > s->srv->cur_sess_max) |
| 860 | s->srv->cur_sess_max = s->srv->cur_sess; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 861 | if (s->be->lbprm.server_take_conn) |
| 862 | s->be->lbprm.server_take_conn(s->srv); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 863 | } |
| 864 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 865 | return SN_ERR_NONE; /* connection is OK */ |
| 866 | } |
| 867 | |
| 868 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 869 | /* This function performs the "redispatch" part of a connection attempt. It |
| 870 | * will assign a server if required, queue the connection if required, and |
| 871 | * handle errors that might arise at this level. It can change the server |
| 872 | * state. It will return 1 if it encounters an error, switches the server |
| 873 | * state, or has to queue a connection. Otherwise, it will return 0 indicating |
| 874 | * that the connection is ready to use. |
| 875 | */ |
| 876 | |
| 877 | int srv_redispatch_connect(struct session *t) |
| 878 | { |
| 879 | int conn_err; |
| 880 | |
| 881 | /* We know that we don't have any connection pending, so we will |
| 882 | * try to get a new one, and wait in this state if it's queued |
| 883 | */ |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 884 | redispatch: |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 885 | conn_err = assign_server_and_queue(t); |
| 886 | switch (conn_err) { |
| 887 | case SRV_STATUS_OK: |
| 888 | break; |
| 889 | |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 890 | case SRV_STATUS_FULL: |
| 891 | /* The server has reached its maxqueue limit. Either PR_O_REDISP is set |
| 892 | * and we can redispatch to another server, or it is not and we return |
| 893 | * 503. This only makes sense in DIRECT mode however, because normal LB |
| 894 | * algorithms would never select such a server, and hash algorithms |
| 895 | * would bring us on the same server again. Note that t->srv is set in |
| 896 | * this case. |
| 897 | */ |
| 898 | if ((t->flags & SN_DIRECT) && (t->be->options & PR_O_REDISP)) { |
| 899 | t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET); |
| 900 | t->prev_srv = t->srv; |
| 901 | goto redispatch; |
| 902 | } |
| 903 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 904 | if (!t->req->cons->err_type) { |
| 905 | t->req->cons->err_type = SI_ET_QUEUE_ERR; |
| 906 | t->req->cons->err_loc = t->srv; |
| 907 | } |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 908 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 909 | t->srv->counters.failed_conns++; |
| 910 | t->be->counters.failed_conns++; |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 911 | return 1; |
| 912 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 913 | case SRV_STATUS_NOSRV: |
| 914 | /* note: it is guaranteed that t->srv == NULL here */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 915 | if (!t->req->cons->err_type) { |
| 916 | t->req->cons->err_type = SI_ET_CONN_ERR; |
| 917 | t->req->cons->err_loc = NULL; |
| 918 | } |
Krzysztof Piotr Oledzki | 5a329cf | 2008-02-22 03:50:19 +0100 | [diff] [blame] | 919 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 920 | t->be->counters.failed_conns++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 921 | return 1; |
| 922 | |
| 923 | case SRV_STATUS_QUEUED: |
Willy Tarreau | 3537467 | 2008-09-03 18:11:02 +0200 | [diff] [blame] | 924 | 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] | 925 | t->req->cons->state = SI_ST_QUE; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 926 | /* do nothing else and do not wake any other session up */ |
| 927 | return 1; |
| 928 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 929 | case SRV_STATUS_INTERNAL: |
| 930 | default: |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 931 | if (!t->req->cons->err_type) { |
| 932 | t->req->cons->err_type = SI_ET_CONN_OTHER; |
| 933 | t->req->cons->err_loc = t->srv; |
| 934 | } |
| 935 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 936 | if (t->srv) |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 937 | srv_inc_sess_ctr(t->srv); |
Willy Tarreau | 98937b8 | 2007-12-10 15:05:42 +0100 | [diff] [blame] | 938 | if (t->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 939 | t->srv->counters.failed_conns++; |
| 940 | t->be->counters.failed_conns++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 941 | |
| 942 | /* release other sessions waiting for this server */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 943 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 944 | process_srv_queue(t->srv); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 945 | return 1; |
| 946 | } |
| 947 | /* if we get here, it's because we got SRV_STATUS_OK, which also |
| 948 | * means that the connection has not been queued. |
| 949 | */ |
| 950 | return 0; |
| 951 | } |
| 952 | |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 953 | int be_downtime(struct proxy *px) { |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 954 | 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] | 955 | return px->down_time; |
| 956 | |
| 957 | return now.tv_sec - px->last_change + px->down_time; |
| 958 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 959 | |
Willy Tarreau | a0cbda6 | 2007-11-01 21:39:54 +0100 | [diff] [blame] | 960 | /* This function parses a "balance" statement in a backend section describing |
| 961 | * <curproxy>. It returns -1 if there is any error, otherwise zero. If it |
| 962 | * returns -1, it may write an error message into ther <err> buffer, for at |
| 963 | * most <errlen> bytes, trailing zero included. The trailing '\n' will not be |
| 964 | * written. The function must be called with <args> pointing to the first word |
| 965 | * after "balance". |
| 966 | */ |
| 967 | int backend_parse_balance(const char **args, char *err, int errlen, struct proxy *curproxy) |
| 968 | { |
| 969 | if (!*(args[0])) { |
| 970 | /* if no option is set, use round-robin by default */ |
Willy Tarreau | 3168223 | 2007-11-29 15:38:04 +0100 | [diff] [blame] | 971 | curproxy->lbprm.algo &= ~BE_LB_ALGO; |
| 972 | curproxy->lbprm.algo |= BE_LB_ALGO_RR; |
Willy Tarreau | a0cbda6 | 2007-11-01 21:39:54 +0100 | [diff] [blame] | 973 | return 0; |
| 974 | } |
| 975 | |
| 976 | if (!strcmp(args[0], "roundrobin")) { |
Willy Tarreau | 3168223 | 2007-11-29 15:38:04 +0100 | [diff] [blame] | 977 | curproxy->lbprm.algo &= ~BE_LB_ALGO; |
| 978 | curproxy->lbprm.algo |= BE_LB_ALGO_RR; |
Willy Tarreau | a0cbda6 | 2007-11-01 21:39:54 +0100 | [diff] [blame] | 979 | } |
Willy Tarreau | 9757a38 | 2009-10-03 12:56:50 +0200 | [diff] [blame] | 980 | else if (!strcmp(args[0], "static-rr")) { |
| 981 | curproxy->lbprm.algo &= ~BE_LB_ALGO; |
| 982 | curproxy->lbprm.algo |= BE_LB_ALGO_SRR; |
| 983 | } |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 984 | else if (!strcmp(args[0], "leastconn")) { |
| 985 | curproxy->lbprm.algo &= ~BE_LB_ALGO; |
| 986 | curproxy->lbprm.algo |= BE_LB_ALGO_LC; |
| 987 | } |
Willy Tarreau | a0cbda6 | 2007-11-01 21:39:54 +0100 | [diff] [blame] | 988 | else if (!strcmp(args[0], "source")) { |
Willy Tarreau | 3168223 | 2007-11-29 15:38:04 +0100 | [diff] [blame] | 989 | curproxy->lbprm.algo &= ~BE_LB_ALGO; |
| 990 | curproxy->lbprm.algo |= BE_LB_ALGO_SH; |
Willy Tarreau | a0cbda6 | 2007-11-01 21:39:54 +0100 | [diff] [blame] | 991 | } |
| 992 | else if (!strcmp(args[0], "uri")) { |
Marek Majkowski | 9c30fc1 | 2008-04-27 23:25:55 +0200 | [diff] [blame] | 993 | int arg = 1; |
| 994 | |
Willy Tarreau | 3168223 | 2007-11-29 15:38:04 +0100 | [diff] [blame] | 995 | curproxy->lbprm.algo &= ~BE_LB_ALGO; |
| 996 | curproxy->lbprm.algo |= BE_LB_ALGO_UH; |
Marek Majkowski | 9c30fc1 | 2008-04-27 23:25:55 +0200 | [diff] [blame] | 997 | |
| 998 | while (*args[arg]) { |
| 999 | if (!strcmp(args[arg], "len")) { |
| 1000 | if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) { |
| 1001 | snprintf(err, errlen, "'balance uri len' expects a positive integer (got '%s').", args[arg+1]); |
| 1002 | return -1; |
| 1003 | } |
| 1004 | curproxy->uri_len_limit = atoi(args[arg+1]); |
| 1005 | arg += 2; |
| 1006 | } |
| 1007 | else if (!strcmp(args[arg], "depth")) { |
| 1008 | if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) { |
| 1009 | snprintf(err, errlen, "'balance uri depth' expects a positive integer (got '%s').", args[arg+1]); |
| 1010 | return -1; |
| 1011 | } |
| 1012 | /* hint: we store the position of the ending '/' (depth+1) so |
| 1013 | * that we avoid a comparison while computing the hash. |
| 1014 | */ |
| 1015 | curproxy->uri_dirs_depth1 = atoi(args[arg+1]) + 1; |
| 1016 | arg += 2; |
| 1017 | } |
| 1018 | else { |
| 1019 | snprintf(err, errlen, "'balance uri' only accepts parameters 'len' and 'depth' (got '%s').", args[arg]); |
| 1020 | return -1; |
| 1021 | } |
| 1022 | } |
Willy Tarreau | a0cbda6 | 2007-11-01 21:39:54 +0100 | [diff] [blame] | 1023 | } |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 1024 | else if (!strcmp(args[0], "url_param")) { |
| 1025 | if (!*args[1]) { |
| 1026 | snprintf(err, errlen, "'balance url_param' requires an URL parameter name."); |
| 1027 | return -1; |
| 1028 | } |
Willy Tarreau | 3168223 | 2007-11-29 15:38:04 +0100 | [diff] [blame] | 1029 | curproxy->lbprm.algo &= ~BE_LB_ALGO; |
| 1030 | curproxy->lbprm.algo |= BE_LB_ALGO_PH; |
Willy Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 1031 | |
| 1032 | free(curproxy->url_param_name); |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 1033 | curproxy->url_param_name = strdup(args[1]); |
Willy Tarreau | a534fea | 2008-08-03 12:19:50 +0200 | [diff] [blame] | 1034 | curproxy->url_param_len = strlen(args[1]); |
Marek Majkowski | 9c30fc1 | 2008-04-27 23:25:55 +0200 | [diff] [blame] | 1035 | if (*args[2]) { |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 1036 | if (strcmp(args[2], "check_post")) { |
| 1037 | snprintf(err, errlen, "'balance url_param' only accepts check_post modifier."); |
| 1038 | return -1; |
| 1039 | } |
| 1040 | if (*args[3]) { |
| 1041 | /* TODO: maybe issue a warning if there is no value, no digits or too long */ |
| 1042 | curproxy->url_param_post_limit = str2ui(args[3]); |
| 1043 | } |
| 1044 | /* if no limit, or faul value in args[3], then default to a moderate wordlen */ |
| 1045 | if (!curproxy->url_param_post_limit) |
| 1046 | curproxy->url_param_post_limit = 48; |
| 1047 | else if ( curproxy->url_param_post_limit < 3 ) |
| 1048 | curproxy->url_param_post_limit = 3; /* minimum example: S=3 or \r\nS=6& */ |
| 1049 | } |
Benoit | affb481 | 2009-03-25 13:02:10 +0100 | [diff] [blame] | 1050 | } |
| 1051 | else if (!strncmp(args[0], "hdr(", 4)) { |
| 1052 | const char *beg, *end; |
| 1053 | |
| 1054 | beg = args[0] + 4; |
| 1055 | end = strchr(beg, ')'); |
| 1056 | |
| 1057 | if (!end || end == beg) { |
| 1058 | snprintf(err, errlen, "'balance hdr(name)' requires an http header field name."); |
| 1059 | return -1; |
| 1060 | } |
| 1061 | |
| 1062 | curproxy->lbprm.algo &= ~BE_LB_ALGO; |
| 1063 | curproxy->lbprm.algo |= BE_LB_ALGO_HH; |
| 1064 | |
| 1065 | free(curproxy->hh_name); |
| 1066 | curproxy->hh_len = end - beg; |
| 1067 | curproxy->hh_name = my_strndup(beg, end - beg); |
| 1068 | curproxy->hh_match_domain = 0; |
| 1069 | |
| 1070 | if (*args[1]) { |
| 1071 | if (strcmp(args[1], "use_domain_only")) { |
| 1072 | snprintf(err, errlen, "'balance hdr(name)' only accepts 'use_domain_only' modifier."); |
| 1073 | return -1; |
| 1074 | } |
| 1075 | curproxy->hh_match_domain = 1; |
| 1076 | } |
| 1077 | |
Emeric Brun | 736aa23 | 2009-06-30 17:56:00 +0200 | [diff] [blame] | 1078 | } |
| 1079 | else if (!strncmp(args[0], "rdp-cookie", 10)) { |
| 1080 | curproxy->lbprm.algo &= ~BE_LB_ALGO; |
| 1081 | curproxy->lbprm.algo |= BE_LB_ALGO_RCH; |
| 1082 | |
| 1083 | if ( *(args[0] + 10 ) == '(' ) { /* cookie name */ |
| 1084 | const char *beg, *end; |
| 1085 | |
| 1086 | beg = args[0] + 11; |
| 1087 | end = strchr(beg, ')'); |
| 1088 | |
| 1089 | if (!end || end == beg) { |
| 1090 | snprintf(err, errlen, "'balance rdp-cookie(name)' requires an rdp cookie name."); |
| 1091 | return -1; |
| 1092 | } |
| 1093 | |
| 1094 | free(curproxy->hh_name); |
| 1095 | curproxy->hh_name = my_strndup(beg, end - beg); |
| 1096 | curproxy->hh_len = end - beg; |
| 1097 | } |
| 1098 | else if ( *(args[0] + 10 ) == '\0' ) { /* default cookie name 'mstshash' */ |
| 1099 | free(curproxy->hh_name); |
| 1100 | curproxy->hh_name = strdup("mstshash"); |
| 1101 | curproxy->hh_len = strlen(curproxy->hh_name); |
| 1102 | } |
| 1103 | else { /* syntax */ |
| 1104 | snprintf(err, errlen, "'balance rdp-cookie(name)' requires an rdp cookie name."); |
| 1105 | return -1; |
| 1106 | } |
Willy Tarreau | 0173280 | 2007-11-01 22:48:15 +0100 | [diff] [blame] | 1107 | } |
Willy Tarreau | a0cbda6 | 2007-11-01 21:39:54 +0100 | [diff] [blame] | 1108 | else { |
Willy Tarreau | 9757a38 | 2009-10-03 12:56:50 +0200 | [diff] [blame] | 1109 | 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] | 1110 | return -1; |
| 1111 | } |
| 1112 | return 0; |
| 1113 | } |
| 1114 | |
Willy Tarreau | a9d3c1e | 2007-11-30 20:48:53 +0100 | [diff] [blame] | 1115 | |
| 1116 | /************************************************************************/ |
| 1117 | /* All supported keywords must be declared here. */ |
| 1118 | /************************************************************************/ |
| 1119 | |
| 1120 | /* set test->i to the number of enabled servers on the proxy */ |
| 1121 | static int |
| 1122 | acl_fetch_nbsrv(struct proxy *px, struct session *l4, void *l7, int dir, |
| 1123 | struct acl_expr *expr, struct acl_test *test) |
| 1124 | { |
| 1125 | test->flags = ACL_TEST_F_VOL_TEST; |
| 1126 | if (expr->arg_len) { |
| 1127 | /* another proxy was designated, we must look for it */ |
| 1128 | for (px = proxy; px; px = px->next) |
| 1129 | if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str)) |
| 1130 | break; |
| 1131 | } |
| 1132 | if (!px) |
| 1133 | return 0; |
| 1134 | |
| 1135 | if (px->srv_act) |
| 1136 | test->i = px->srv_act; |
| 1137 | else if (px->lbprm.fbck) |
| 1138 | test->i = 1; |
| 1139 | else |
| 1140 | test->i = px->srv_bck; |
| 1141 | |
| 1142 | return 1; |
| 1143 | } |
| 1144 | |
Jeffrey 'jf' Lim | 5051d7b | 2008-09-04 01:03:03 +0800 | [diff] [blame] | 1145 | /* set test->i to the number of enabled servers on the proxy */ |
| 1146 | static int |
| 1147 | acl_fetch_connslots(struct proxy *px, struct session *l4, void *l7, int dir, |
| 1148 | struct acl_expr *expr, struct acl_test *test) |
| 1149 | { |
| 1150 | struct server *iterator; |
| 1151 | test->flags = ACL_TEST_F_VOL_TEST; |
| 1152 | if (expr->arg_len) { |
| 1153 | /* another proxy was designated, we must look for it */ |
| 1154 | for (px = proxy; px; px = px->next) |
| 1155 | if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str)) |
| 1156 | break; |
| 1157 | } |
| 1158 | if (!px) |
| 1159 | return 0; |
| 1160 | |
| 1161 | test->i = 0; |
| 1162 | iterator = px->srv; |
| 1163 | while (iterator) { |
| 1164 | if ((iterator->state & 1) == 0) { |
| 1165 | iterator = iterator->next; |
| 1166 | continue; |
| 1167 | } |
| 1168 | if (iterator->maxconn == 0 || iterator->maxqueue == 0) { |
| 1169 | test->i = -1; |
| 1170 | return 1; |
| 1171 | } |
| 1172 | |
| 1173 | test->i += (iterator->maxconn - iterator->cur_sess) |
| 1174 | + (iterator->maxqueue - iterator->nbpend); |
| 1175 | iterator = iterator->next; |
| 1176 | } |
| 1177 | |
| 1178 | return 1; |
| 1179 | } |
| 1180 | |
Willy Tarreau | 079ff0a | 2009-03-05 21:34:28 +0100 | [diff] [blame] | 1181 | /* set test->i to the number of connections per second reaching the frontend */ |
| 1182 | static int |
| 1183 | acl_fetch_fe_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir, |
| 1184 | struct acl_expr *expr, struct acl_test *test) |
| 1185 | { |
| 1186 | test->flags = ACL_TEST_F_VOL_TEST; |
| 1187 | if (expr->arg_len) { |
| 1188 | /* another proxy was designated, we must look for it */ |
| 1189 | for (px = proxy; px; px = px->next) |
| 1190 | if ((px->cap & PR_CAP_FE) && !strcmp(px->id, expr->arg.str)) |
| 1191 | break; |
| 1192 | } |
| 1193 | if (!px) |
| 1194 | return 0; |
| 1195 | |
| 1196 | test->i = read_freq_ctr(&px->fe_sess_per_sec); |
| 1197 | return 1; |
| 1198 | } |
| 1199 | |
| 1200 | /* set test->i to the number of connections per second reaching the backend */ |
| 1201 | static int |
| 1202 | acl_fetch_be_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir, |
| 1203 | struct acl_expr *expr, struct acl_test *test) |
| 1204 | { |
| 1205 | test->flags = ACL_TEST_F_VOL_TEST; |
| 1206 | if (expr->arg_len) { |
| 1207 | /* another proxy was designated, we must look for it */ |
| 1208 | for (px = proxy; px; px = px->next) |
| 1209 | if ((px->cap & PR_CAP_BE) && !strcmp(px->id, expr->arg.str)) |
| 1210 | break; |
| 1211 | } |
| 1212 | if (!px) |
| 1213 | return 0; |
| 1214 | |
| 1215 | test->i = read_freq_ctr(&px->be_sess_per_sec); |
| 1216 | return 1; |
| 1217 | } |
| 1218 | |
Willy Tarreau | a9d3c1e | 2007-11-30 20:48:53 +0100 | [diff] [blame] | 1219 | |
| 1220 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 1221 | static struct acl_kw_list acl_kws = {{ },{ |
Jeffrey 'jf' Lim | 5051d7b | 2008-09-04 01:03:03 +0800 | [diff] [blame] | 1222 | { "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] | 1223 | { "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] | 1224 | { "fe_sess_rate", acl_parse_int, acl_fetch_fe_sess_rate, acl_match_int, ACL_USE_NOTHING }, |
| 1225 | { "be_sess_rate", acl_parse_int, acl_fetch_be_sess_rate, acl_match_int, ACL_USE_NOTHING }, |
Willy Tarreau | a9d3c1e | 2007-11-30 20:48:53 +0100 | [diff] [blame] | 1226 | { NULL, NULL, NULL, NULL }, |
| 1227 | }}; |
| 1228 | |
| 1229 | |
| 1230 | __attribute__((constructor)) |
| 1231 | static void __backend_init(void) |
| 1232 | { |
| 1233 | acl_register_keywords(&acl_kws); |
| 1234 | } |
| 1235 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1236 | /* |
| 1237 | * Local variables: |
| 1238 | * c-indent-level: 8 |
| 1239 | * c-basic-offset: 8 |
| 1240 | * End: |
| 1241 | */ |