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