s4:dsdb/common: prepare dsdb_user_obj_set_defaults() for tombstone reanimation
[Samba.git] / ctdb / server / ipalloc.c
blobdd88f81c29bf008a0b4c936806436ac6cd77828e
1 /*
2 ctdb ip takeover code
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/>.
22 #include <talloc.h>
24 #include "replace.h"
25 #include "system/network.h"
27 #include "lib/util/debug.h"
29 #include "common/logging.h"
30 #include "common/rb_tree.h"
32 #include "server/ipalloc_private.h"
34 /* Initialise main ipalloc state and sub-structures */
35 struct ipalloc_state *
36 ipalloc_state_init(TALLOC_CTX *mem_ctx,
37 uint32_t num_nodes,
38 enum ipalloc_algorithm algorithm,
39 bool no_ip_failback,
40 uint32_t *force_rebalance_nodes)
42 struct ipalloc_state *ipalloc_state =
43 talloc_zero(mem_ctx, struct ipalloc_state);
44 if (ipalloc_state == NULL) {
45 DEBUG(DEBUG_ERR, (__location__ " Out of memory\n"));
46 return NULL;
49 ipalloc_state->num = num_nodes;
51 ipalloc_state->noiptakeover =
52 talloc_zero_array(ipalloc_state,
53 bool,
54 ipalloc_state->num);
55 if (ipalloc_state->noiptakeover == NULL) {
56 DEBUG(DEBUG_ERR, (__location__ " Out of memory\n"));
57 goto fail;
59 ipalloc_state->noiphost =
60 talloc_zero_array(ipalloc_state,
61 bool,
62 ipalloc_state->num);
63 if (ipalloc_state->noiphost == NULL) {
64 DEBUG(DEBUG_ERR, (__location__ " Out of memory\n"));
65 goto fail;
68 ipalloc_state->algorithm = algorithm;
69 ipalloc_state->no_ip_failback = no_ip_failback;
70 ipalloc_state->force_rebalance_nodes = force_rebalance_nodes;
72 return ipalloc_state;
73 fail:
74 talloc_free(ipalloc_state);
75 return NULL;
78 static void *add_ip_callback(void *parm, void *data)
80 struct public_ip_list *this_ip = parm;
81 struct public_ip_list *prev_ip = data;
83 if (prev_ip == NULL) {
84 return parm;
86 if (this_ip->pnn == -1) {
87 this_ip->pnn = prev_ip->pnn;
90 return parm;
93 static int getips_count_callback(void *param, void *data)
95 struct public_ip_list **ip_list = (struct public_ip_list **)param;
96 struct public_ip_list *new_ip = (struct public_ip_list *)data;
98 new_ip->next = *ip_list;
99 *ip_list = new_ip;
100 return 0;
103 /* Nodes only know about those public addresses that they are
104 * configured to serve and no individual node has a full list of all
105 * public addresses configured across the cluster. Therefore, a
106 * merged list of all public addresses needs to be built so that IP
107 * allocation can be done. */
108 static struct public_ip_list *
109 create_merged_ip_list(struct ipalloc_state *ipalloc_state,
110 struct ctdb_public_ip_list *known_ips)
112 int i, j;
113 struct public_ip_list *ip_list;
114 struct ctdb_public_ip_list *public_ips;
115 struct trbt_tree *ip_tree;
117 ip_tree = trbt_create(ipalloc_state, 0);
119 if (known_ips == NULL) {
120 DEBUG(DEBUG_ERR, ("Known public IPs not set\n"));
121 return NULL;
124 for (i=0; i < ipalloc_state->num; i++) {
126 public_ips = &known_ips[i];
128 for (j=0; j < public_ips->num; j++) {
129 struct public_ip_list *tmp_ip;
131 /* This is returned as part of ip_list */
132 tmp_ip = talloc_zero(ipalloc_state, struct public_ip_list);
133 if (tmp_ip == NULL) {
134 DEBUG(DEBUG_ERR,
135 (__location__ " out of memory\n"));
136 talloc_free(ip_tree);
137 return NULL;
140 /* Do not use information about IP addresses hosted
141 * on other nodes, it may not be accurate */
142 if (public_ips->ip[j].pnn == i) {
143 tmp_ip->pnn = public_ips->ip[j].pnn;
144 } else {
145 tmp_ip->pnn = -1;
147 tmp_ip->addr = public_ips->ip[j].addr;
148 tmp_ip->next = NULL;
150 trbt_insertarray32_callback(ip_tree,
151 IP_KEYLEN, ip_key(&public_ips->ip[j].addr),
152 add_ip_callback,
153 tmp_ip);
157 ip_list = NULL;
158 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_count_callback, &ip_list);
159 talloc_free(ip_tree);
161 return ip_list;
164 static bool all_nodes_are_disabled(struct ctdb_node_map *nodemap)
166 int i;
168 for (i=0;i<nodemap->num;i++) {
169 if (!(nodemap->node[i].flags &
170 (NODE_FLAGS_INACTIVE|NODE_FLAGS_DISABLED))) {
171 /* Found one completely healthy node */
172 return false;
176 return true;
179 /* Set internal flags for IP allocation:
180 * Clear ip flags
181 * Set NOIPTAKOVER ip flags from per-node NoIPTakeover tunable
182 * Set NOIPHOST ip flag for each INACTIVE node
183 * if all nodes are disabled:
184 * Set NOIPHOST ip flags from per-node NoIPHostOnAllDisabled tunable
185 * else
186 * Set NOIPHOST ip flags for disabled nodes
188 void ipalloc_set_node_flags(struct ipalloc_state *ipalloc_state,
189 struct ctdb_node_map *nodemap,
190 uint32_t *tval_noiptakeover,
191 uint32_t *tval_noiphostonalldisabled)
193 int i;
195 for (i=0;i<nodemap->num;i++) {
196 /* Can not take IPs on node with NoIPTakeover set */
197 if (tval_noiptakeover[i] != 0) {
198 ipalloc_state->noiptakeover[i] = true;
201 /* Can not host IPs on INACTIVE node */
202 if (nodemap->node[i].flags & NODE_FLAGS_INACTIVE) {
203 ipalloc_state->noiphost[i] = true;
207 if (all_nodes_are_disabled(nodemap)) {
208 /* If all nodes are disabled, can not host IPs on node
209 * with NoIPHostOnAllDisabled set
211 for (i=0;i<nodemap->num;i++) {
212 if (tval_noiphostonalldisabled[i] != 0) {
213 ipalloc_state->noiphost[i] = true;
216 } else {
217 /* If some nodes are not disabled, then can not host
218 * IPs on DISABLED node
220 for (i=0;i<nodemap->num;i++) {
221 if (nodemap->node[i].flags & NODE_FLAGS_DISABLED) {
222 ipalloc_state->noiphost[i] = true;
228 bool ipalloc_set_public_ips(struct ipalloc_state *ipalloc_state,
229 struct ctdb_public_ip_list *known_ips,
230 struct ctdb_public_ip_list *available_ips)
232 ipalloc_state->available_public_ips = available_ips;
234 ipalloc_state->all_ips = create_merged_ip_list(ipalloc_state,
235 known_ips);
237 return (ipalloc_state->all_ips != NULL);
240 /* This can only return false if there are no available IPs *and*
241 * there are no IP addresses currently allocated. If the latter is
242 * true then the cluster can clearly host IPs... just not necessarily
243 * right now... */
244 bool ipalloc_can_host_ips(struct ipalloc_state *ipalloc_state)
246 int i;
247 struct public_ip_list *ip_list;
250 for (ip_list = ipalloc_state->all_ips;
251 ip_list != NULL;
252 ip_list = ip_list->next) {
253 if (ip_list->pnn != -1) {
254 return true;
258 for (i=0; i < ipalloc_state->num; i++) {
259 if (ipalloc_state->available_public_ips[i].num != 0) {
260 return true;
264 return false;
267 /* The calculation part of the IP allocation algorithm. */
268 struct public_ip_list *ipalloc(struct ipalloc_state *ipalloc_state)
270 bool ret = false;
272 switch (ipalloc_state->algorithm) {
273 case IPALLOC_LCP2:
274 ret = ipalloc_lcp2(ipalloc_state);
275 break;
276 case IPALLOC_DETERMINISTIC:
277 ret = ipalloc_deterministic(ipalloc_state);
278 break;
279 case IPALLOC_NONDETERMINISTIC:
280 ret = ipalloc_nondeterministic(ipalloc_state);
281 break;
284 /* at this point ->pnn is the node which will own each IP
285 or -1 if there is no node that can cover this ip
288 return (ret ? ipalloc_state->all_ips : NULL);