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