idmap_hash: mirror the NT_STATUS_NONE_MAPPED/STATUS_SOME_UNMAPPED logic from idmap_au...
[Samba.git] / ctdb / server / ipalloc.c
blob7f4936439da86cf566555b063db0132483c3d6e5
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 "replace.h"
23 #include "system/network.h"
25 #include <talloc.h>
27 #include "lib/util/debug.h"
29 #include "common/logging.h"
30 #include "common/rb_tree.h"
32 #include "protocol/protocol_util.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,
39 uint32_t num_nodes,
40 enum ipalloc_algorithm algorithm,
41 bool no_ip_takeover,
42 bool no_ip_failback,
43 uint32_t *force_rebalance_nodes)
45 struct ipalloc_state *ipalloc_state =
46 talloc_zero(mem_ctx, struct ipalloc_state);
47 if (ipalloc_state == NULL) {
48 DEBUG(DEBUG_ERR, (__location__ " Out of memory\n"));
49 return NULL;
52 ipalloc_state->num = num_nodes;
54 ipalloc_state->algorithm = algorithm;
55 ipalloc_state->no_ip_takeover = no_ip_takeover;
56 ipalloc_state->no_ip_failback = no_ip_failback;
57 ipalloc_state->force_rebalance_nodes = force_rebalance_nodes;
59 return ipalloc_state;
62 static void *add_ip_callback(void *parm, void *data)
64 struct public_ip_list *this_ip = parm;
65 struct public_ip_list *prev_ip = data;
67 if (prev_ip == NULL) {
68 return parm;
70 if (this_ip->pnn == CTDB_UNKNOWN_PNN) {
71 this_ip->pnn = prev_ip->pnn;
74 return parm;
77 static int getips_count_callback(void *param, void *data)
79 struct public_ip_list **ip_list = (struct public_ip_list **)param;
80 struct public_ip_list *new_ip = (struct public_ip_list *)data;
82 new_ip->next = *ip_list;
83 *ip_list = new_ip;
84 return 0;
87 /* Nodes only know about those public addresses that they are
88 * configured to serve and no individual node has a full list of all
89 * public addresses configured across the cluster. Therefore, a
90 * merged list of all public addresses needs to be built so that IP
91 * allocation can be done. */
92 static struct public_ip_list *
93 create_merged_ip_list(struct ipalloc_state *ipalloc_state)
95 unsigned int i, j;
96 struct public_ip_list *ip_list;
97 struct ctdb_public_ip_list *public_ips;
98 struct trbt_tree *ip_tree;
99 int ret;
101 ip_tree = trbt_create(ipalloc_state, 0);
103 if (ipalloc_state->known_public_ips == NULL) {
104 DEBUG(DEBUG_ERR, ("Known public IPs not set\n"));
105 return NULL;
108 for (i=0; i < ipalloc_state->num; i++) {
110 public_ips = &ipalloc_state->known_public_ips[i];
112 for (j=0; j < public_ips->num; j++) {
113 struct public_ip_list *tmp_ip;
115 /* This is returned as part of ip_list */
116 tmp_ip = talloc_zero(ipalloc_state, struct public_ip_list);
117 if (tmp_ip == NULL) {
118 DEBUG(DEBUG_ERR,
119 (__location__ " out of memory\n"));
120 talloc_free(ip_tree);
121 return NULL;
124 /* Do not use information about IP addresses hosted
125 * on other nodes, it may not be accurate */
126 if (public_ips->ip[j].pnn == i) {
127 tmp_ip->pnn = public_ips->ip[j].pnn;
128 } else {
129 tmp_ip->pnn = CTDB_UNKNOWN_PNN;
131 tmp_ip->addr = public_ips->ip[j].addr;
132 tmp_ip->next = NULL;
134 trbt_insertarray32_callback(ip_tree,
135 IP_KEYLEN, ip_key(&public_ips->ip[j].addr),
136 add_ip_callback,
137 tmp_ip);
141 ip_list = NULL;
142 ret = trbt_traversearray32(ip_tree, IP_KEYLEN, getips_count_callback, &ip_list);
143 if (ret != 0) {
144 DBG_ERR("Error traversing the IP tree.\n");
147 talloc_free(ip_tree);
149 return ip_list;
152 static bool populate_bitmap(struct ipalloc_state *ipalloc_state)
154 struct public_ip_list *ip = NULL;
155 unsigned int i, j;
157 for (ip = ipalloc_state->all_ips; ip != NULL; ip = ip->next) {
159 ip->known_on = bitmap_talloc(ip, ipalloc_state->num);
160 if (ip->known_on == NULL) {
161 return false;
164 ip->available_on = bitmap_talloc(ip, ipalloc_state->num);
165 if (ip->available_on == NULL) {
166 return false;
169 for (i = 0; i < ipalloc_state->num; i++) {
170 struct ctdb_public_ip_list *known =
171 &ipalloc_state->known_public_ips[i];
172 struct ctdb_public_ip_list *avail =
173 &ipalloc_state->available_public_ips[i];
175 /* Check to see if "ip" is available on node "i" */
176 for (j = 0; j < avail->num; j++) {
177 if (ctdb_sock_addr_same_ip(
178 &ip->addr, &avail->ip[j].addr)) {
179 bitmap_set(ip->available_on, i);
180 break;
184 /* Optimisation: available => known */
185 if (bitmap_query(ip->available_on, i)) {
186 bitmap_set(ip->known_on, i);
187 continue;
190 /* Check to see if "ip" is known on node "i" */
191 for (j = 0; j < known->num; j++) {
192 if (ctdb_sock_addr_same_ip(
193 &ip->addr, &known->ip[j].addr)) {
194 bitmap_set(ip->known_on, i);
195 break;
201 return true;
204 void ipalloc_set_public_ips(struct ipalloc_state *ipalloc_state,
205 struct ctdb_public_ip_list *known_ips,
206 struct ctdb_public_ip_list *available_ips)
208 ipalloc_state->available_public_ips = available_ips;
209 ipalloc_state->known_public_ips = known_ips;
212 /* This can only return false if there are no available IPs *and*
213 * there are no IP addresses currently allocated. If the latter is
214 * true then the cluster can clearly host IPs... just not necessarily
215 * right now... */
216 bool ipalloc_can_host_ips(struct ipalloc_state *ipalloc_state)
218 unsigned int i;
219 bool have_ips = false;
221 for (i=0; i < ipalloc_state->num; i++) {
222 struct ctdb_public_ip_list *ips =
223 ipalloc_state->known_public_ips;
224 if (ips[i].num != 0) {
225 unsigned int j;
226 have_ips = true;
227 /* Succeed if an address is hosted on node i */
228 for (j=0; j < ips[i].num; j++) {
229 if (ips[i].ip[j].pnn == i) {
230 return true;
236 if (! have_ips) {
237 return false;
240 /* At this point there are known addresses but none are
241 * hosted. Need to check if cluster can now host some
242 * addresses.
244 for (i=0; i < ipalloc_state->num; i++) {
245 if (ipalloc_state->available_public_ips[i].num != 0) {
246 return true;
250 return false;
253 /* The calculation part of the IP allocation algorithm. */
254 struct public_ip_list *ipalloc(struct ipalloc_state *ipalloc_state)
256 bool ret = false;
258 ipalloc_state->all_ips = create_merged_ip_list(ipalloc_state);
259 if (ipalloc_state->all_ips == NULL) {
260 return NULL;
263 if (!populate_bitmap(ipalloc_state)) {
264 return NULL;
267 switch (ipalloc_state->algorithm) {
268 case IPALLOC_LCP2:
269 ret = ipalloc_lcp2(ipalloc_state);
270 break;
271 case IPALLOC_DETERMINISTIC:
272 ret = ipalloc_deterministic(ipalloc_state);
273 break;
274 case IPALLOC_NONDETERMINISTIC:
275 ret = ipalloc_nondeterministic(ipalloc_state);
276 break;
279 /* at this point ->pnn is the node which will own each IP
280 or CTDB_UNKNOWN_PNN if there is no node that can cover this ip
283 return (ret ? ipalloc_state->all_ips : NULL);