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