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