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