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