lib:ldb: Use correct integer types for sizes
[vlendec/samba-autobuild/.git] / ctdb / server / ipalloc.c
1 /*
2    ctdb ip takeover code
3
4    Copyright (C) Ronnie Sahlberg  2007
5    Copyright (C) Andrew Tridgell  2007
6    Copyright (C) Martin Schwenke  2011
7
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.
12
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.
17
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/>.
20 */
21
22 #include "replace.h"
23 #include "system/network.h"
24
25 #include <talloc.h>
26
27 #include "lib/util/debug.h"
28
29 #include "common/logging.h"
30 #include "common/rb_tree.h"
31
32 #include "protocol/protocol_util.h"
33
34 #include "server/ipalloc_private.h"
35
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)
44 {
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;
50         }
51
52         ipalloc_state->num = num_nodes;
53
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;
58
59         return ipalloc_state;
60 }
61
62 static void *add_ip_callback(void *parm, void *data)
63 {
64         struct public_ip_list *this_ip = parm;
65         struct public_ip_list *prev_ip = data;
66
67         if (prev_ip == NULL) {
68                 return parm;
69         }
70         if (this_ip->pnn == CTDB_UNKNOWN_PNN) {
71                 this_ip->pnn = prev_ip->pnn;
72         }
73
74         return parm;
75 }
76
77 static int getips_count_callback(void *param, void *data)
78 {
79         struct public_ip_list **ip_list = (struct public_ip_list **)param;
80         struct public_ip_list *new_ip = (struct public_ip_list *)data;
81
82         new_ip->next = *ip_list;
83         *ip_list     = new_ip;
84         return 0;
85 }
86
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)
94 {
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;
100
101         ip_tree = trbt_create(ipalloc_state, 0);
102
103         if (ipalloc_state->known_public_ips == NULL) {
104                 DEBUG(DEBUG_ERR, ("Known public IPs not set\n"));
105                 return NULL;
106         }
107
108         for (i=0; i < ipalloc_state->num; i++) {
109
110                 public_ips = &ipalloc_state->known_public_ips[i];
111
112                 for (j=0; j < public_ips->num; j++) {
113                         struct public_ip_list *tmp_ip;
114
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;
122                         }
123
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;
130                         }
131                         tmp_ip->addr = public_ips->ip[j].addr;
132                         tmp_ip->next = NULL;
133
134                         trbt_insertarray32_callback(ip_tree,
135                                 IP_KEYLEN, ip_key(&public_ips->ip[j].addr),
136                                 add_ip_callback,
137                                 tmp_ip);
138                 }
139         }
140
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");
145         }
146
147         talloc_free(ip_tree);
148
149         return ip_list;
150 }
151
152 static bool populate_bitmap(struct ipalloc_state *ipalloc_state)
153 {
154         struct public_ip_list *ip = NULL;
155         unsigned int i, j;
156
157         for (ip = ipalloc_state->all_ips; ip != NULL; ip = ip->next) {
158
159                 ip->known_on = bitmap_talloc(ip, ipalloc_state->num);
160                 if (ip->known_on == NULL) {
161                         return false;
162                 }
163
164                 ip->available_on = bitmap_talloc(ip, ipalloc_state->num);
165                 if (ip->available_on == NULL) {
166                         return false;
167                 }
168
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];
174
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;
181                                 }
182                         }
183
184                         /* Optimisation: available => known */
185                         if (bitmap_query(ip->available_on, i)) {
186                                 bitmap_set(ip->known_on, i);
187                                 continue;
188                         }
189
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;
196                                 }
197                         }
198                 }
199         }
200
201         return true;
202 }
203
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)
207 {
208         ipalloc_state->available_public_ips = available_ips;
209         ipalloc_state->known_public_ips = known_ips;
210 }
211
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)
217 {
218         unsigned int i;
219         bool have_ips = false;
220
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;
231                                 }
232                         }
233                 }
234         }
235
236         if (! have_ips) {
237                 return false;
238         }
239
240         /* At this point there are known addresses but none are
241          * hosted.  Need to check if cluster can now host some
242          * addresses.
243          */
244         for (i=0; i < ipalloc_state->num; i++) {
245                 if (ipalloc_state->available_public_ips[i].num != 0) {
246                         return true;
247                 }
248         }
249
250         return false;
251 }
252
253 /* The calculation part of the IP allocation algorithm. */
254 struct public_ip_list *ipalloc(struct ipalloc_state *ipalloc_state)
255 {
256         bool ret = false;
257
258         ipalloc_state->all_ips = create_merged_ip_list(ipalloc_state);
259         if (ipalloc_state->all_ips == NULL) {
260                 return NULL;
261         }
262
263         if (!populate_bitmap(ipalloc_state)) {
264                 return NULL;
265         }
266
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;
277         }
278
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
281         */
282
283         return (ret ? ipalloc_state->all_ips : NULL);
284 }