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