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