4 Copyright (C) Ronnie Sahlberg 2007
5 Copyright (C) Andrew Tridgell 2007
6 Copyright (C) Martin Schwenke 2011
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, see <http://www.gnu.org/licenses/>.
25 #include "system/network.h"
27 #include "lib/util/debug.h"
29 #include "common/logging.h"
30 #include "common/rb_tree.h"
32 #include "protocol/protocol_api.h"
34 #include "server/ipalloc_private.h"
36 /* Initialise main ipalloc state and sub-structures */
37 struct ipalloc_state *
38 ipalloc_state_init(TALLOC_CTX *mem_ctx,
40 enum ipalloc_algorithm algorithm,
43 bool no_ip_host_on_all_disabled,
44 uint32_t *force_rebalance_nodes)
46 struct ipalloc_state *ipalloc_state =
47 talloc_zero(mem_ctx, struct ipalloc_state);
48 if (ipalloc_state == NULL) {
49 DEBUG(DEBUG_ERR, (__location__ " Out of memory\n"));
53 ipalloc_state->num = num_nodes;
55 ipalloc_state->noiphost =
56 talloc_zero_array(ipalloc_state,
59 if (ipalloc_state->noiphost == NULL) {
60 DEBUG(DEBUG_ERR, (__location__ " Out of memory\n"));
64 ipalloc_state->algorithm = algorithm;
65 ipalloc_state->no_ip_takeover = no_ip_takeover;
66 ipalloc_state->no_ip_failback = no_ip_failback;
67 ipalloc_state->no_ip_host_on_all_disabled = no_ip_host_on_all_disabled;
68 ipalloc_state->force_rebalance_nodes = force_rebalance_nodes;
72 talloc_free(ipalloc_state);
76 static void *add_ip_callback(void *parm, void *data)
78 struct public_ip_list *this_ip = parm;
79 struct public_ip_list *prev_ip = data;
81 if (prev_ip == NULL) {
84 if (this_ip->pnn == -1) {
85 this_ip->pnn = prev_ip->pnn;
91 static int getips_count_callback(void *param, void *data)
93 struct public_ip_list **ip_list = (struct public_ip_list **)param;
94 struct public_ip_list *new_ip = (struct public_ip_list *)data;
96 new_ip->next = *ip_list;
101 /* Nodes only know about those public addresses that they are
102 * configured to serve and no individual node has a full list of all
103 * public addresses configured across the cluster. Therefore, a
104 * merged list of all public addresses needs to be built so that IP
105 * allocation can be done. */
106 static struct public_ip_list *
107 create_merged_ip_list(struct ipalloc_state *ipalloc_state)
110 struct public_ip_list *ip_list;
111 struct ctdb_public_ip_list *public_ips;
112 struct trbt_tree *ip_tree;
114 ip_tree = trbt_create(ipalloc_state, 0);
116 if (ipalloc_state->known_public_ips == NULL) {
117 DEBUG(DEBUG_ERR, ("Known public IPs not set\n"));
121 for (i=0; i < ipalloc_state->num; i++) {
123 public_ips = &ipalloc_state->known_public_ips[i];
125 for (j=0; j < public_ips->num; j++) {
126 struct public_ip_list *tmp_ip;
128 /* This is returned as part of ip_list */
129 tmp_ip = talloc_zero(ipalloc_state, struct public_ip_list);
130 if (tmp_ip == NULL) {
132 (__location__ " out of memory\n"));
133 talloc_free(ip_tree);
137 /* Do not use information about IP addresses hosted
138 * on other nodes, it may not be accurate */
139 if (public_ips->ip[j].pnn == i) {
140 tmp_ip->pnn = public_ips->ip[j].pnn;
144 tmp_ip->addr = public_ips->ip[j].addr;
147 trbt_insertarray32_callback(ip_tree,
148 IP_KEYLEN, ip_key(&public_ips->ip[j].addr),
155 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_count_callback, &ip_list);
156 talloc_free(ip_tree);
161 static bool populate_bitmap(struct ipalloc_state *ipalloc_state)
163 struct public_ip_list *ip = NULL;
166 for (ip = ipalloc_state->all_ips; ip != NULL; ip = ip->next) {
168 ip->available_on = talloc_zero_array(ip, bool,
170 if (ip->available_on == NULL) {
174 for (i = 0; i < ipalloc_state->num; i++) {
175 struct ctdb_public_ip_list *avail =
176 &ipalloc_state->available_public_ips[i];
178 /* Check to see if "ip" is available on node "i" */
179 for (j = 0; j < avail->num; j++) {
180 if (ctdb_sock_addr_same_ip(
181 &ip->addr, &avail->ip[j].addr)) {
182 ip->available_on[i] = true;
192 static bool all_nodes_are_disabled(struct ctdb_node_map *nodemap)
196 for (i=0;i<nodemap->num;i++) {
197 if (!(nodemap->node[i].flags &
198 (NODE_FLAGS_INACTIVE|NODE_FLAGS_DISABLED))) {
199 /* Found one completely healthy node */
207 /* Set internal flags for IP allocation:
209 * Set NOIPHOST ip flag for each INACTIVE node
210 * if all nodes are disabled:
211 * Set NOIPHOST ip flags from per-node NoIPHostOnAllDisabled tunable
213 * Set NOIPHOST ip flags for disabled nodes
215 void ipalloc_set_node_flags(struct ipalloc_state *ipalloc_state,
216 struct ctdb_node_map *nodemap)
219 bool all_disabled = all_nodes_are_disabled(nodemap);
221 for (i=0;i<nodemap->num;i++) {
222 /* Can not host IPs on INACTIVE node */
223 if (nodemap->node[i].flags & NODE_FLAGS_INACTIVE) {
224 ipalloc_state->noiphost[i] = true;
227 /* If node is disabled then it can only host IPs if
228 * all nodes are disabled and NoIPHostOnAllDisabled is
231 if (nodemap->node[i].flags & NODE_FLAGS_DISABLED) {
232 if (!(all_disabled &&
233 ipalloc_state->no_ip_host_on_all_disabled == 0)) {
235 ipalloc_state->noiphost[i] = true;
241 void ipalloc_set_public_ips(struct ipalloc_state *ipalloc_state,
242 struct ctdb_public_ip_list *known_ips,
243 struct ctdb_public_ip_list *available_ips)
245 ipalloc_state->available_public_ips = available_ips;
246 ipalloc_state->known_public_ips = known_ips;
249 /* This can only return false if there are no available IPs *and*
250 * there are no IP addresses currently allocated. If the latter is
251 * true then the cluster can clearly host IPs... just not necessarily
253 bool ipalloc_can_host_ips(struct ipalloc_state *ipalloc_state)
256 bool have_ips = false;
258 for (i=0; i < ipalloc_state->num; i++) {
259 struct ctdb_public_ip_list *ips =
260 ipalloc_state->known_public_ips;
261 if (ips[i].num != 0) {
264 /* Succeed if an address is hosted on node i */
265 for (j=0; j < ips[i].num; j++) {
266 if (ips[i].ip[j].pnn == i) {
277 /* At this point there are known addresses but none are
278 * hosted. Need to check if cluster can now host some
281 for (i=0; i < ipalloc_state->num; i++) {
282 if (ipalloc_state->available_public_ips[i].num != 0) {
290 /* The calculation part of the IP allocation algorithm. */
291 struct public_ip_list *ipalloc(struct ipalloc_state *ipalloc_state)
295 ipalloc_state->all_ips = create_merged_ip_list(ipalloc_state);
296 if (ipalloc_state->all_ips == NULL) {
300 if (!populate_bitmap(ipalloc_state)) {
304 switch (ipalloc_state->algorithm) {
306 ret = ipalloc_lcp2(ipalloc_state);
308 case IPALLOC_DETERMINISTIC:
309 ret = ipalloc_deterministic(ipalloc_state);
311 case IPALLOC_NONDETERMINISTIC:
312 ret = ipalloc_nondeterministic(ipalloc_state);
316 /* at this point ->pnn is the node which will own each IP
317 or -1 if there is no node that can cover this ip
320 return (ret ? ipalloc_state->all_ips : NULL);