librpc/tables.pl: remove unused $opt_output option
[Samba.git] / ctdb / server / ipalloc.c
blobdce2b03cad29ee6227f39c77913645e5f064a5a8
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 == -1) {
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 int i, j;
96 struct public_ip_list *ip_list;
97 struct ctdb_public_ip_list *public_ips;
98 struct trbt_tree *ip_tree;
100 ip_tree = trbt_create(ipalloc_state, 0);
102 if (ipalloc_state->known_public_ips == NULL) {
103 DEBUG(DEBUG_ERR, ("Known public IPs not set\n"));
104 return NULL;
107 for (i=0; i < ipalloc_state->num; i++) {
109 public_ips = &ipalloc_state->known_public_ips[i];
111 for (j=0; j < public_ips->num; j++) {
112 struct public_ip_list *tmp_ip;
114 /* This is returned as part of ip_list */
115 tmp_ip = talloc_zero(ipalloc_state, struct public_ip_list);
116 if (tmp_ip == NULL) {
117 DEBUG(DEBUG_ERR,
118 (__location__ " out of memory\n"));
119 talloc_free(ip_tree);
120 return NULL;
123 /* Do not use information about IP addresses hosted
124 * on other nodes, it may not be accurate */
125 if (public_ips->ip[j].pnn == i) {
126 tmp_ip->pnn = public_ips->ip[j].pnn;
127 } else {
128 tmp_ip->pnn = -1;
130 tmp_ip->addr = public_ips->ip[j].addr;
131 tmp_ip->next = NULL;
133 trbt_insertarray32_callback(ip_tree,
134 IP_KEYLEN, ip_key(&public_ips->ip[j].addr),
135 add_ip_callback,
136 tmp_ip);
140 ip_list = NULL;
141 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_count_callback, &ip_list);
142 talloc_free(ip_tree);
144 return ip_list;
147 static bool populate_bitmap(struct ipalloc_state *ipalloc_state)
149 struct public_ip_list *ip = NULL;
150 int i, j;
152 for (ip = ipalloc_state->all_ips; ip != NULL; ip = ip->next) {
154 ip->known_on = bitmap_talloc(ip, ipalloc_state->num);
155 if (ip->known_on == NULL) {
156 return false;
159 ip->available_on = bitmap_talloc(ip, ipalloc_state->num);
160 if (ip->available_on == NULL) {
161 return false;
164 for (i = 0; i < ipalloc_state->num; i++) {
165 struct ctdb_public_ip_list *known =
166 &ipalloc_state->known_public_ips[i];
167 struct ctdb_public_ip_list *avail =
168 &ipalloc_state->available_public_ips[i];
170 /* Check to see if "ip" is available on node "i" */
171 for (j = 0; j < avail->num; j++) {
172 if (ctdb_sock_addr_same_ip(
173 &ip->addr, &avail->ip[j].addr)) {
174 bitmap_set(ip->available_on, i);
175 break;
179 /* Optimisation: available => known */
180 if (bitmap_query(ip->available_on, i)) {
181 bitmap_set(ip->known_on, i);
182 continue;
185 /* Check to see if "ip" is known on node "i" */
186 for (j = 0; j < known->num; j++) {
187 if (ctdb_sock_addr_same_ip(
188 &ip->addr, &known->ip[j].addr)) {
189 bitmap_set(ip->known_on, i);
190 break;
196 return true;
199 void ipalloc_set_public_ips(struct ipalloc_state *ipalloc_state,
200 struct ctdb_public_ip_list *known_ips,
201 struct ctdb_public_ip_list *available_ips)
203 ipalloc_state->available_public_ips = available_ips;
204 ipalloc_state->known_public_ips = known_ips;
207 /* This can only return false if there are no available IPs *and*
208 * there are no IP addresses currently allocated. If the latter is
209 * true then the cluster can clearly host IPs... just not necessarily
210 * right now... */
211 bool ipalloc_can_host_ips(struct ipalloc_state *ipalloc_state)
213 int i;
214 bool have_ips = false;
216 for (i=0; i < ipalloc_state->num; i++) {
217 struct ctdb_public_ip_list *ips =
218 ipalloc_state->known_public_ips;
219 if (ips[i].num != 0) {
220 int j;
221 have_ips = true;
222 /* Succeed if an address is hosted on node i */
223 for (j=0; j < ips[i].num; j++) {
224 if (ips[i].ip[j].pnn == i) {
225 return true;
231 if (! have_ips) {
232 return false;
235 /* At this point there are known addresses but none are
236 * hosted. Need to check if cluster can now host some
237 * addresses.
239 for (i=0; i < ipalloc_state->num; i++) {
240 if (ipalloc_state->available_public_ips[i].num != 0) {
241 return true;
245 return false;
248 /* The calculation part of the IP allocation algorithm. */
249 struct public_ip_list *ipalloc(struct ipalloc_state *ipalloc_state)
251 bool ret = false;
253 ipalloc_state->all_ips = create_merged_ip_list(ipalloc_state);
254 if (ipalloc_state->all_ips == NULL) {
255 return NULL;
258 if (!populate_bitmap(ipalloc_state)) {
259 return NULL;
262 switch (ipalloc_state->algorithm) {
263 case IPALLOC_LCP2:
264 ret = ipalloc_lcp2(ipalloc_state);
265 break;
266 case IPALLOC_DETERMINISTIC:
267 ret = ipalloc_deterministic(ipalloc_state);
268 break;
269 case IPALLOC_NONDETERMINISTIC:
270 ret = ipalloc_nondeterministic(ipalloc_state);
271 break;
274 /* at this point ->pnn is the node which will own each IP
275 or -1 if there is no node that can cover this ip
278 return (ret ? ipalloc_state->all_ips : NULL);