summaryrefslogtreecommitdiff
path: root/ctdb/server/ipalloc.c
blob: caa50224156eb7a1b8eea5c6e03102c6a7d6fee8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
   ctdb ip takeover code

   Copyright (C) Ronnie Sahlberg  2007
   Copyright (C) Andrew Tridgell  2007
   Copyright (C) Martin Schwenke  2011

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, see <http://www.gnu.org/licenses/>.
*/

#include <talloc.h>

#include "replace.h"
#include "system/network.h"

#include "lib/util/debug.h"

#include "common/logging.h"
#include "common/rb_tree.h"

#include "protocol/protocol_util.h"

#include "server/ipalloc_private.h"

/* Initialise main ipalloc state and sub-structures */
struct ipalloc_state *
ipalloc_state_init(TALLOC_CTX *mem_ctx,
		   uint32_t num_nodes,
		   enum ipalloc_algorithm algorithm,
		   bool no_ip_takeover,
		   bool no_ip_failback,
		   bool no_ip_host_on_all_disabled,
		   uint32_t *force_rebalance_nodes)
{
	struct ipalloc_state *ipalloc_state =
		talloc_zero(mem_ctx, struct ipalloc_state);
	if (ipalloc_state == NULL) {
		DEBUG(DEBUG_ERR, (__location__ " Out of memory\n"));
		return NULL;
	}

	ipalloc_state->num = num_nodes;

	ipalloc_state->noiphost = bitmap_talloc(ipalloc_state,
						ipalloc_state->num);
	if (ipalloc_state->noiphost == NULL) {
		DEBUG(DEBUG_ERR, (__location__ " Out of memory\n"));
		goto fail;
	}

	ipalloc_state->algorithm = algorithm;
	ipalloc_state->no_ip_takeover = no_ip_takeover;
	ipalloc_state->no_ip_failback = no_ip_failback;
	ipalloc_state->no_ip_host_on_all_disabled = no_ip_host_on_all_disabled;
	ipalloc_state->force_rebalance_nodes = force_rebalance_nodes;

	return ipalloc_state;
fail:
	talloc_free(ipalloc_state);
	return NULL;
}

static void *add_ip_callback(void *parm, void *data)
{
	struct public_ip_list *this_ip = parm;
	struct public_ip_list *prev_ip = data;

	if (prev_ip == NULL) {
		return parm;
	}
	if (this_ip->pnn == -1) {
		this_ip->pnn = prev_ip->pnn;
	}

	return parm;
}

static int getips_count_callback(void *param, void *data)
{
	struct public_ip_list **ip_list = (struct public_ip_list **)param;
	struct public_ip_list *new_ip = (struct public_ip_list *)data;

	new_ip->next = *ip_list;
	*ip_list     = new_ip;
	return 0;
}

/* Nodes only know about those public addresses that they are
 * configured to serve and no individual node has a full list of all
 * public addresses configured across the cluster.  Therefore, a
 * merged list of all public addresses needs to be built so that IP
 * allocation can be done. */
static struct public_ip_list *
create_merged_ip_list(struct ipalloc_state *ipalloc_state)
{
	int i, j;
	struct public_ip_list *ip_list;
	struct ctdb_public_ip_list *public_ips;
	struct trbt_tree *ip_tree;

	ip_tree = trbt_create(ipalloc_state, 0);

	if (ipalloc_state->known_public_ips == NULL) {
		DEBUG(DEBUG_ERR, ("Known public IPs not set\n"));
		return NULL;
	}

	for (i=0; i < ipalloc_state->num; i++) {

		public_ips = &ipalloc_state->known_public_ips[i];

		for (j=0; j < public_ips->num; j++) {
			struct public_ip_list *tmp_ip;

			/* This is returned as part of ip_list */
			tmp_ip = talloc_zero(ipalloc_state, struct public_ip_list);
			if (tmp_ip == NULL) {
				DEBUG(DEBUG_ERR,
				      (__location__ " out of memory\n"));
				talloc_free(ip_tree);
				return NULL;
			}

			/* Do not use information about IP addresses hosted
			 * on other nodes, it may not be accurate */
			if (public_ips->ip[j].pnn == i) {
				tmp_ip->pnn = public_ips->ip[j].pnn;
			} else {
				tmp_ip->pnn = -1;
			}
			tmp_ip->addr = public_ips->ip[j].addr;
			tmp_ip->next = NULL;

			trbt_insertarray32_callback(ip_tree,
				IP_KEYLEN, ip_key(&public_ips->ip[j].addr),
				add_ip_callback,
				tmp_ip);
		}
	}

	ip_list = NULL;
	trbt_traversearray32(ip_tree, IP_KEYLEN, getips_count_callback, &ip_list);
	talloc_free(ip_tree);

	return ip_list;
}

static bool populate_bitmap(struct ipalloc_state *ipalloc_state)
{
	struct public_ip_list *ip = NULL;
	int i, j;

	for (ip = ipalloc_state->all_ips; ip != NULL; ip = ip->next) {

		ip->known_on = bitmap_talloc(ip, ipalloc_state->num);
		if (ip->known_on == NULL) {
			return false;
		}

		ip->available_on = bitmap_talloc(ip, ipalloc_state->num);
		if (ip->available_on == NULL) {
			return false;
		}

		for (i = 0; i < ipalloc_state->num; i++) {
			struct ctdb_public_ip_list *known =
				&ipalloc_state->known_public_ips[i];
			struct ctdb_public_ip_list *avail =
				&ipalloc_state->available_public_ips[i];

			/* Check to see if "ip" is available on node "i" */
			for (j = 0; j < avail->num; j++) {
				if (ctdb_sock_addr_same_ip(
					    &ip->addr, &avail->ip[j].addr)) {
					bitmap_set(ip->available_on, i);
					break;
				}
			}

			/* Optimisation: available => known */
			if (bitmap_query(ip->available_on, i)) {
				bitmap_set(ip->known_on, i);
				continue;
			}

			/* Check to see if "ip" is known on node "i" */
			for (j = 0; j < known->num; j++) {
				if (ctdb_sock_addr_same_ip(
					    &ip->addr, &known->ip[j].addr)) {
					bitmap_set(ip->known_on, i);
					break;
				}
			}
		}
	}

	return true;
}

static bool all_nodes_are_disabled(struct ctdb_node_map *nodemap)
{
	int i;

	for (i=0;i<nodemap->num;i++) {
		if (!(nodemap->node[i].flags &
		      (NODE_FLAGS_INACTIVE|NODE_FLAGS_DISABLED))) {
			/* Found one completely healthy node */
			return false;
		}
	}

	return true;
}

/* Set internal flags for IP allocation:
 *   Clear ip flags
 *   Set NOIPHOST ip flag for each INACTIVE node
 *   if all nodes are disabled:
 *     Set NOIPHOST ip flags from per-node NoIPHostOnAllDisabled tunable
 *   else
 *     Set NOIPHOST ip flags for disabled nodes
 */
void ipalloc_set_node_flags(struct ipalloc_state *ipalloc_state,
			    struct ctdb_node_map *nodemap)
{
	int i;
	bool all_disabled = all_nodes_are_disabled(nodemap);

	for (i=0;i<nodemap->num;i++) {
		/* Can not host IPs on INACTIVE node */
		if (nodemap->node[i].flags & NODE_FLAGS_INACTIVE) {
			bitmap_set(ipalloc_state->noiphost, i);
		}

		/* If node is disabled then it can only host IPs if
		 * all nodes are disabled and NoIPHostOnAllDisabled is
		 * unset
		 */
		if (nodemap->node[i].flags & NODE_FLAGS_DISABLED) {
			if (!(all_disabled &&
			      ipalloc_state->no_ip_host_on_all_disabled == 0)) {

				bitmap_set(ipalloc_state->noiphost, i);
			}
		}
	}
}

void ipalloc_set_public_ips(struct ipalloc_state *ipalloc_state,
			    struct ctdb_public_ip_list *known_ips,
			    struct ctdb_public_ip_list *available_ips)
{
	ipalloc_state->available_public_ips = available_ips;
	ipalloc_state->known_public_ips = known_ips;
}

/* This can only return false if there are no available IPs *and*
 * there are no IP addresses currently allocated.  If the latter is
 * true then the cluster can clearly host IPs... just not necessarily
 * right now... */
bool ipalloc_can_host_ips(struct ipalloc_state *ipalloc_state)
{
	int i;
	bool have_ips = false;

	for (i=0; i < ipalloc_state->num; i++) {
		struct ctdb_public_ip_list *ips =
			ipalloc_state->known_public_ips;
		if (ips[i].num != 0) {
			int j;
			have_ips = true;
			/* Succeed if an address is hosted on node i */
			for (j=0; j < ips[i].num; j++) {
				if (ips[i].ip[j].pnn == i) {
					return true;
				}
			}
		}
	}

	if (! have_ips) {
		return false;
	}

	/* At this point there are known addresses but none are
	 * hosted.  Need to check if cluster can now host some
	 * addresses.
	 */
	for (i=0; i < ipalloc_state->num; i++) {
		if (ipalloc_state->available_public_ips[i].num != 0) {
			return true;
		}
	}

	return false;
}

/* The calculation part of the IP allocation algorithm. */
struct public_ip_list *ipalloc(struct ipalloc_state *ipalloc_state)
{
	bool ret = false;

	ipalloc_state->all_ips = create_merged_ip_list(ipalloc_state);
	if (ipalloc_state->all_ips == NULL) {
		return NULL;
	}

	if (!populate_bitmap(ipalloc_state)) {
		return NULL;
	}

	switch (ipalloc_state->algorithm) {
	case IPALLOC_LCP2:
		ret = ipalloc_lcp2(ipalloc_state);
		break;
	case IPALLOC_DETERMINISTIC:
		ret = ipalloc_deterministic(ipalloc_state);
		break;
	case IPALLOC_NONDETERMINISTIC:
		ret = ipalloc_nondeterministic(ipalloc_state);
               break;
	}

	/* at this point ->pnn is the node which will own each IP
	   or -1 if there is no node that can cover this ip
	*/

	return (ret ? ipalloc_state->all_ips : NULL);
}