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