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 | 2fcb500 | 2007-05-08 13:35:26 +0200 | [diff] [blame] | 5 | Copyright (C) 2000-2007 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); |
| 36 | int srv_count_retry_down(struct session *t, int conn_err); |
| 37 | int srv_retryable_connect(struct session *t); |
| 38 | int srv_redispatch_connect(struct session *t); |
Willy Tarreau | a0cbda6 | 2007-11-01 21:39:54 +0100 | [diff] [blame] | 39 | int backend_parse_balance(const char **args, char *err, |
| 40 | int errlen, struct proxy *curproxy); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 41 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 42 | void recalc_server_map(struct proxy *px); |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 43 | int be_downtime(struct proxy *px); |
Willy Tarreau | 5dc2fa6 | 2007-11-19 19:10:18 +0100 | [diff] [blame] | 44 | void init_server_map(struct proxy *p); |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 45 | void fwrr_init_server_groups(struct proxy *p); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 46 | |
| 47 | /* |
| 48 | * This function tries to find a running server with free connection slots for |
| 49 | * the proxy <px> following the round-robin method. |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 50 | * 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] | 51 | * to point to the next server. If no valid server is found, NULL is returned. |
| 52 | */ |
| 53 | static inline struct server *get_server_rr_with_conns(struct proxy *px) |
| 54 | { |
| 55 | int newidx; |
| 56 | struct server *srv; |
| 57 | |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 58 | if (px->lbprm.tot_weight == 0) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 59 | return NULL; |
| 60 | |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 61 | if (px->lbprm.map.state & PR_MAP_RECALC) |
| 62 | recalc_server_map(px); |
| 63 | |
| 64 | if (px->lbprm.map.rr_idx < 0 || px->lbprm.map.rr_idx >= px->lbprm.tot_weight) |
| 65 | px->lbprm.map.rr_idx = 0; |
| 66 | newidx = px->lbprm.map.rr_idx; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 67 | |
| 68 | do { |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 69 | srv = px->lbprm.map.srv[newidx++]; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 70 | if (!srv->maxconn || srv->cur_sess < srv_dynamic_maxconn(srv)) { |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 71 | px->lbprm.map.rr_idx = newidx; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 72 | return srv; |
| 73 | } |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 74 | if (newidx == px->lbprm.tot_weight) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 75 | newidx = 0; |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 76 | } while (newidx != px->lbprm.map.rr_idx); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 77 | |
| 78 | return NULL; |
| 79 | } |
| 80 | |
| 81 | |
| 82 | /* |
| 83 | * This function tries to find a running server for the proxy <px> following |
| 84 | * the round-robin method. |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 85 | * 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] | 86 | * to point to the next server. If no valid server is found, NULL is returned. |
| 87 | */ |
| 88 | static inline struct server *get_server_rr(struct proxy *px) |
| 89 | { |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 90 | if (px->lbprm.tot_weight == 0) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 91 | return NULL; |
| 92 | |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 93 | if (px->lbprm.map.state & PR_MAP_RECALC) |
| 94 | recalc_server_map(px); |
| 95 | |
| 96 | if (px->lbprm.map.rr_idx < 0 || px->lbprm.map.rr_idx >= px->lbprm.tot_weight) |
| 97 | px->lbprm.map.rr_idx = 0; |
| 98 | return px->lbprm.map.srv[px->lbprm.map.rr_idx++]; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | |
| 102 | /* |
| 103 | * This function tries to find a running server for the proxy <px> following |
| 104 | * the source hash method. Depending on the number of active/backup servers, |
| 105 | * it will either look for active servers, or for backup servers. |
| 106 | * If any server is found, it will be returned. If no valid server is found, |
| 107 | * NULL is returned. |
| 108 | */ |
Willy Tarreau | 5af3a69 | 2007-07-24 23:32:33 +0200 | [diff] [blame] | 109 | static inline struct server *get_server_sh(struct proxy *px, |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 110 | const char *addr, int len) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 111 | { |
| 112 | unsigned int h, l; |
| 113 | |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 114 | if (px->lbprm.tot_weight == 0) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 115 | return NULL; |
| 116 | |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 117 | if (px->lbprm.map.state & PR_MAP_RECALC) |
| 118 | recalc_server_map(px); |
| 119 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 120 | l = h = 0; |
Willy Tarreau | b625a08 | 2007-11-26 01:15:43 +0100 | [diff] [blame] | 121 | |
| 122 | /* note: we won't hash if there's only one server left */ |
| 123 | if (px->lbprm.tot_used > 1) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 124 | while ((l + sizeof (int)) <= len) { |
| 125 | h ^= ntohl(*(unsigned int *)(&addr[l])); |
| 126 | l += sizeof (int); |
| 127 | } |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 128 | h %= px->lbprm.tot_weight; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 129 | } |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 130 | return px->lbprm.map.srv[h]; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 131 | } |
| 132 | |
Willy Tarreau | 2fcb500 | 2007-05-08 13:35:26 +0200 | [diff] [blame] | 133 | /* |
| 134 | * This function tries to find a running server for the proxy <px> following |
| 135 | * the URI hash method. In order to optimize cache hits, the hash computation |
| 136 | * ends at the question mark. Depending on the number of active/backup servers, |
| 137 | * it will either look for active servers, or for backup servers. |
| 138 | * If any server is found, it will be returned. If no valid server is found, |
| 139 | * NULL is returned. |
| 140 | * |
| 141 | * This code was contributed by Guillaume Dallaire, who also selected this hash |
| 142 | * algorithm out of a tens because it gave him the best results. |
| 143 | * |
| 144 | */ |
| 145 | static inline struct server *get_server_uh(struct proxy *px, char *uri, int uri_len) |
| 146 | { |
| 147 | unsigned long hash = 0; |
| 148 | int c; |
| 149 | |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 150 | if (px->lbprm.tot_weight == 0) |
Willy Tarreau | 2fcb500 | 2007-05-08 13:35:26 +0200 | [diff] [blame] | 151 | return NULL; |
| 152 | |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 153 | if (px->lbprm.map.state & PR_MAP_RECALC) |
| 154 | recalc_server_map(px); |
| 155 | |
Willy Tarreau | 2fcb500 | 2007-05-08 13:35:26 +0200 | [diff] [blame] | 156 | while (uri_len--) { |
| 157 | c = *uri++; |
| 158 | if (c == '?') |
| 159 | break; |
| 160 | hash = c + (hash << 6) + (hash << 16) - hash; |
| 161 | } |
| 162 | |
Willy Tarreau | 2069704 | 2007-11-15 23:26:18 +0100 | [diff] [blame] | 163 | return px->lbprm.map.srv[hash % px->lbprm.tot_weight]; |
Willy Tarreau | 2fcb500 | 2007-05-08 13:35:26 +0200 | [diff] [blame] | 164 | } |
| 165 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 166 | |
| 167 | #endif /* _PROTO_BACKEND_H */ |
| 168 | |
| 169 | /* |
| 170 | * Local variables: |
| 171 | * c-indent-level: 8 |
| 172 | * c-basic-offset: 8 |
| 173 | * End: |
| 174 | */ |