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