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