Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | include/proto/backend.h |
| 3 | Functions prototypes for the backend. |
| 4 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 5 | Copyright (C) 2000-2008 Willy Tarreau - w@1wt.eu |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6 | |
| 7 | This library is free software; you can redistribute it and/or |
| 8 | modify it under the terms of the GNU Lesser General Public |
| 9 | License as published by the Free Software Foundation, version 2.1 |
| 10 | exclusively. |
| 11 | |
| 12 | This library is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | Lesser General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU Lesser General Public |
| 18 | License along with this library; if not, write to the Free Software |
| 19 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | #ifndef _PROTO_BACKEND_H |
| 23 | #define _PROTO_BACKEND_H |
| 24 | |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 25 | #include <common/config.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 26 | |
| 27 | #include <types/backend.h> |
| 28 | #include <types/session.h> |
| 29 | |
| 30 | #include <proto/queue.h> |
| 31 | |
| 32 | int assign_server(struct session *s); |
| 33 | int assign_server_address(struct session *s); |
| 34 | int assign_server_and_queue(struct session *s); |
| 35 | int connect_server(struct session *s); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 36 | int srv_redispatch_connect(struct session *t); |
Willy Tarreau | a0cbda6 | 2007-11-01 21:39:54 +0100 | [diff] [blame] | 37 | int backend_parse_balance(const char **args, char *err, |
| 38 | int errlen, struct proxy *curproxy); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 39 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 40 | void recalc_server_map(struct proxy *px); |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 41 | int be_downtime(struct proxy *px); |
Willy Tarreau | 5dc2fa6 | 2007-11-19 19:10:18 +0100 | [diff] [blame] | 42 | void init_server_map(struct proxy *p); |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 43 | void fwrr_init_server_groups(struct proxy *p); |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 44 | void fwlc_init_server_tree(struct proxy *p); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 45 | |
| 46 | /* |
| 47 | * This function tries to find a running server with free connection slots for |
| 48 | * the proxy <px> following the round-robin method. |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 49 | * If any server is found, it will be returned and px->lbprm.map.rr_idx will be updated |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 50 | * to point to the next server. If no valid server is found, NULL is returned. |
| 51 | */ |
Krzysztof Piotr Oledzki | 5a329cf | 2008-02-22 03:50:19 +0100 | [diff] [blame] | 52 | static inline struct server *get_server_rr_with_conns(struct proxy *px, struct server *srvtoavoid) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 53 | { |
Krzysztof Piotr Oledzki | 5a329cf | 2008-02-22 03:50:19 +0100 | [diff] [blame] | 54 | int newidx, avoididx; |
| 55 | struct server *srv, *avoided; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 56 | |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 57 | if (px->lbprm.tot_weight == 0) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 58 | return NULL; |
| 59 | |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 60 | if (px->lbprm.map.state & PR_MAP_RECALC) |
| 61 | recalc_server_map(px); |
| 62 | |
| 63 | if (px->lbprm.map.rr_idx < 0 || px->lbprm.map.rr_idx >= px->lbprm.tot_weight) |
| 64 | px->lbprm.map.rr_idx = 0; |
| 65 | newidx = px->lbprm.map.rr_idx; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 66 | |
Krzysztof Piotr Oledzki | 5a329cf | 2008-02-22 03:50:19 +0100 | [diff] [blame] | 67 | avoided = NULL; |
Willy Tarreau | f863ac1 | 2008-03-04 06:38:57 +0100 | [diff] [blame] | 68 | avoididx = 0; /* shut a gcc warning */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 69 | do { |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 70 | srv = px->lbprm.map.srv[newidx++]; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 71 | if (!srv->maxconn || srv->cur_sess < srv_dynamic_maxconn(srv)) { |
Krzysztof Piotr Oledzki | 5a329cf | 2008-02-22 03:50:19 +0100 | [diff] [blame] | 72 | /* make sure it is not the server we are try to exclude... */ |
| 73 | if (srv != srvtoavoid) { |
| 74 | px->lbprm.map.rr_idx = newidx; |
| 75 | return srv; |
| 76 | } |
| 77 | |
| 78 | avoided = srv; /* ...but remember that is was selected yet avoided */ |
| 79 | avoididx = newidx; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 80 | } |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 81 | if (newidx == px->lbprm.tot_weight) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 82 | newidx = 0; |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 83 | } while (newidx != px->lbprm.map.rr_idx); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 84 | |
Krzysztof Piotr Oledzki | 5a329cf | 2008-02-22 03:50:19 +0100 | [diff] [blame] | 85 | if (avoided) |
| 86 | px->lbprm.map.rr_idx = avoididx; |
| 87 | |
| 88 | /* return NULL or srvtoavoid if found */ |
| 89 | return avoided; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | |
| 93 | /* |
| 94 | * This function tries to find a running server for the proxy <px> following |
| 95 | * the round-robin method. |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 96 | * If any server is found, it will be returned and px->lbprm.map.rr_idx will be updated |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 97 | * to point to the next server. If no valid server is found, NULL is returned. |
| 98 | */ |
| 99 | static inline struct server *get_server_rr(struct proxy *px) |
| 100 | { |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 101 | if (px->lbprm.tot_weight == 0) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 102 | return NULL; |
| 103 | |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 104 | if (px->lbprm.map.state & PR_MAP_RECALC) |
| 105 | recalc_server_map(px); |
| 106 | |
| 107 | if (px->lbprm.map.rr_idx < 0 || px->lbprm.map.rr_idx >= px->lbprm.tot_weight) |
| 108 | px->lbprm.map.rr_idx = 0; |
| 109 | return px->lbprm.map.srv[px->lbprm.map.rr_idx++]; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | |
| 113 | /* |
| 114 | * This function tries to find a running server for the proxy <px> following |
| 115 | * the source hash method. Depending on the number of active/backup servers, |
| 116 | * it will either look for active servers, or for backup servers. |
| 117 | * If any server is found, it will be returned. If no valid server is found, |
| 118 | * NULL is returned. |
| 119 | */ |
Willy Tarreau | 5af3a69 | 2007-07-24 23:32:33 +0200 | [diff] [blame] | 120 | static inline struct server *get_server_sh(struct proxy *px, |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 121 | const char *addr, int len) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 122 | { |
| 123 | unsigned int h, l; |
| 124 | |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 125 | if (px->lbprm.tot_weight == 0) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 126 | return NULL; |
| 127 | |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 128 | if (px->lbprm.map.state & PR_MAP_RECALC) |
| 129 | recalc_server_map(px); |
| 130 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 131 | l = h = 0; |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 132 | |
| 133 | /* note: we won't hash if there's only one server left */ |
| 134 | if (px->lbprm.tot_used > 1) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 135 | while ((l + sizeof (int)) <= len) { |
| 136 | h ^= ntohl(*(unsigned int *)(&addr[l])); |
| 137 | l += sizeof (int); |
| 138 | } |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 139 | h %= px->lbprm.tot_weight; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 140 | } |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 141 | return px->lbprm.map.srv[h]; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 142 | } |
| 143 | |
Willy Tarreau | 2fcb500 | 2007-05-08 13:35:26 +0200 | [diff] [blame] | 144 | /* |
| 145 | * This function tries to find a running server for the proxy <px> following |
| 146 | * the URI hash method. In order to optimize cache hits, the hash computation |
| 147 | * ends at the question mark. Depending on the number of active/backup servers, |
| 148 | * it will either look for active servers, or for backup servers. |
| 149 | * If any server is found, it will be returned. If no valid server is found, |
| 150 | * NULL is returned. |
| 151 | * |
| 152 | * This code was contributed by Guillaume Dallaire, who also selected this hash |
| 153 | * algorithm out of a tens because it gave him the best results. |
| 154 | * |
| 155 | */ |
| 156 | static inline struct server *get_server_uh(struct proxy *px, char *uri, int uri_len) |
| 157 | { |
| 158 | unsigned long hash = 0; |
| 159 | int c; |
Marek Majkowski | 9c30fc1 | 2008-04-27 23:25:55 +0200 | [diff] [blame] | 160 | int slashes = 0; |
Willy Tarreau | 2fcb500 | 2007-05-08 13:35:26 +0200 | [diff] [blame] | 161 | |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 162 | if (px->lbprm.tot_weight == 0) |
Willy Tarreau | 2fcb500 | 2007-05-08 13:35:26 +0200 | [diff] [blame] | 163 | return NULL; |
| 164 | |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 165 | if (px->lbprm.map.state & PR_MAP_RECALC) |
| 166 | recalc_server_map(px); |
| 167 | |
Marek Majkowski | 9c30fc1 | 2008-04-27 23:25:55 +0200 | [diff] [blame] | 168 | if (px->uri_len_limit) |
| 169 | uri_len = MIN(uri_len, px->uri_len_limit); |
| 170 | |
Willy Tarreau | 2fcb500 | 2007-05-08 13:35:26 +0200 | [diff] [blame] | 171 | while (uri_len--) { |
| 172 | c = *uri++; |
Marek Majkowski | 9c30fc1 | 2008-04-27 23:25:55 +0200 | [diff] [blame] | 173 | if (c == '/') { |
| 174 | slashes++; |
| 175 | if (slashes == px->uri_dirs_depth1) /* depth+1 */ |
| 176 | break; |
| 177 | } |
| 178 | else if (c == '?') |
Willy Tarreau | 2fcb500 | 2007-05-08 13:35:26 +0200 | [diff] [blame] | 179 | break; |
Marek Majkowski | 9c30fc1 | 2008-04-27 23:25:55 +0200 | [diff] [blame] | 180 | |
Willy Tarreau | 2fcb500 | 2007-05-08 13:35:26 +0200 | [diff] [blame] | 181 | hash = c + (hash << 6) + (hash << 16) - hash; |
| 182 | } |
| 183 | |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 184 | return px->lbprm.map.srv[hash % px->lbprm.tot_weight]; |
Willy Tarreau | 2fcb500 | 2007-05-08 13:35:26 +0200 | [diff] [blame] | 185 | } |
| 186 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 187 | |
| 188 | #endif /* _PROTO_BACKEND_H */ |
| 189 | |
| 190 | /* |
| 191 | * Local variables: |
| 192 | * c-indent-level: 8 |
| 193 | * c-basic-offset: 8 |
| 194 | * End: |
| 195 | */ |