e416c743c2df3d5de9ad4395eeb0877bff5dd966
[metze/samba/wip.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 <talloc.h>
23
24 #include "replace.h"
25 #include "system/network.h"
26
27 #include "lib/util/debug.h"
28
29 #include "common/logging.h"
30 #include "common/rb_tree.h"
31
32 #include "server/ipalloc_private.h"
33
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)
41 {
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;
47         }
48
49         ipalloc_state->num = num_nodes;
50
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;
58         }
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;
66         }
67
68         ipalloc_state->algorithm = algorithm;
69         ipalloc_state->no_ip_failback = no_ip_failback;
70         ipalloc_state->force_rebalance_nodes = force_rebalance_nodes;
71
72         return ipalloc_state;
73 fail:
74         talloc_free(ipalloc_state);
75         return NULL;
76 }
77
78 static void *add_ip_callback(void *parm, void *data)
79 {
80         struct public_ip_list *this_ip = parm;
81         struct public_ip_list *prev_ip = data;
82
83         if (prev_ip == NULL) {
84                 return parm;
85         }
86         if (this_ip->pnn == -1) {
87                 this_ip->pnn = prev_ip->pnn;
88         }
89
90         return parm;
91 }
92
93 static int getips_count_callback(void *param, void *data)
94 {
95         struct public_ip_list **ip_list = (struct public_ip_list **)param;
96         struct public_ip_list *new_ip = (struct public_ip_list *)data;
97
98         new_ip->next = *ip_list;
99         *ip_list     = new_ip;
100         return 0;
101 }
102
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 {
111         int i, j;
112         struct public_ip_list *ip_list;
113         struct ctdb_public_ip_list *public_ips;
114         struct trbt_tree *ip_tree;
115
116         ip_tree = trbt_create(ipalloc_state, 0);
117
118         if (ipalloc_state->known_public_ips == NULL) {
119                 DEBUG(DEBUG_ERR, ("Known public IPs not set\n"));
120                 return NULL;
121         }
122
123         for (i=0; i < ipalloc_state->num; i++) {
124
125                 public_ips = &ipalloc_state->known_public_ips[i];
126
127                 for (j=0; j < public_ips->num; j++) {
128                         struct public_ip_list *tmp_ip;
129
130                         /* This is returned as part of ip_list */
131                         tmp_ip = talloc_zero(ipalloc_state, struct public_ip_list);
132                         if (tmp_ip == NULL) {
133                                 DEBUG(DEBUG_ERR,
134                                       (__location__ " out of memory\n"));
135                                 talloc_free(ip_tree);
136                                 return NULL;
137                         }
138
139                         /* Do not use information about IP addresses hosted
140                          * on other nodes, it may not be accurate */
141                         if (public_ips->ip[j].pnn == i) {
142                                 tmp_ip->pnn = public_ips->ip[j].pnn;
143                         } else {
144                                 tmp_ip->pnn = -1;
145                         }
146                         tmp_ip->addr = public_ips->ip[j].addr;
147                         tmp_ip->next = NULL;
148
149                         trbt_insertarray32_callback(ip_tree,
150                                 IP_KEYLEN, ip_key(&public_ips->ip[j].addr),
151                                 add_ip_callback,
152                                 tmp_ip);
153                 }
154         }
155
156         ip_list = NULL;
157         trbt_traversearray32(ip_tree, IP_KEYLEN, getips_count_callback, &ip_list);
158         talloc_free(ip_tree);
159
160         return ip_list;
161 }
162
163 static bool all_nodes_are_disabled(struct ctdb_node_map *nodemap)
164 {
165         int i;
166
167         for (i=0;i<nodemap->num;i++) {
168                 if (!(nodemap->node[i].flags &
169                       (NODE_FLAGS_INACTIVE|NODE_FLAGS_DISABLED))) {
170                         /* Found one completely healthy node */
171                         return false;
172                 }
173         }
174
175         return true;
176 }
177
178 /* Set internal flags for IP allocation:
179  *   Clear ip flags
180  *   Set NOIPTAKOVER ip flags from per-node NoIPTakeover tunable
181  *   Set NOIPHOST ip flag for each INACTIVE node
182  *   if all nodes are disabled:
183  *     Set NOIPHOST ip flags from per-node NoIPHostOnAllDisabled tunable
184  *   else
185  *     Set NOIPHOST ip flags for disabled nodes
186  */
187 void ipalloc_set_node_flags(struct ipalloc_state *ipalloc_state,
188                             struct ctdb_node_map *nodemap,
189                             uint32_t *tval_noiptakeover,
190                             uint32_t *tval_noiphostonalldisabled)
191 {
192         int i;
193
194         for (i=0;i<nodemap->num;i++) {
195                 /* Can not take IPs on node with NoIPTakeover set */
196                 if (tval_noiptakeover[i] != 0) {
197                         ipalloc_state->noiptakeover[i] = true;
198                 }
199
200                 /* Can not host IPs on INACTIVE node */
201                 if (nodemap->node[i].flags & NODE_FLAGS_INACTIVE) {
202                         ipalloc_state->noiphost[i] = true;
203                 }
204         }
205
206         if (all_nodes_are_disabled(nodemap)) {
207                 /* If all nodes are disabled, can not host IPs on node
208                  * with NoIPHostOnAllDisabled set
209                  */
210                 for (i=0;i<nodemap->num;i++) {
211                         if (tval_noiphostonalldisabled[i] != 0) {
212                                 ipalloc_state->noiphost[i] = true;
213                         }
214                 }
215         } else {
216                 /* If some nodes are not disabled, then can not host
217                  * IPs on DISABLED node
218                  */
219                 for (i=0;i<nodemap->num;i++) {
220                         if (nodemap->node[i].flags & NODE_FLAGS_DISABLED) {
221                                 ipalloc_state->noiphost[i] = true;
222                         }
223                 }
224         }
225 }
226
227 bool ipalloc_set_public_ips(struct ipalloc_state *ipalloc_state,
228                             struct ctdb_public_ip_list *known_ips,
229                             struct ctdb_public_ip_list *available_ips)
230 {
231         ipalloc_state->available_public_ips = available_ips;
232         ipalloc_state->known_public_ips = known_ips;
233
234         ipalloc_state->all_ips = create_merged_ip_list(ipalloc_state);
235
236         return (ipalloc_state->all_ips != NULL);
237 }
238
239 /* This can only return false if there are no available IPs *and*
240  * there are no IP addresses currently allocated.  If the latter is
241  * true then the cluster can clearly host IPs... just not necessarily
242  * right now... */
243 bool ipalloc_can_host_ips(struct ipalloc_state *ipalloc_state)
244 {
245         int i;
246         bool have_ips = false;
247
248         for (i=0; i < ipalloc_state->num; i++) {
249                 struct ctdb_public_ip_list *ips =
250                         ipalloc_state->known_public_ips;
251                 if (ips[i].num != 0) {
252                         int j;
253                         have_ips = true;
254                         /* Succeed if an address is hosted on node i */
255                         for (j=0; j < ips[i].num; j++) {
256                                 if (ips[i].ip[j].pnn == i) {
257                                         return true;
258                                 }
259                         }
260                 }
261         }
262
263         if (! have_ips) {
264                 return false;
265         }
266
267         /* At this point there are known addresses but none are
268          * hosted.  Need to check if cluster can now host some
269          * addresses.
270          */
271         for (i=0; i < ipalloc_state->num; i++) {
272                 if (ipalloc_state->available_public_ips[i].num != 0) {
273                         return true;
274                 }
275         }
276
277         return false;
278 }
279
280 /* The calculation part of the IP allocation algorithm. */
281 struct public_ip_list *ipalloc(struct ipalloc_state *ipalloc_state)
282 {
283         bool ret = false;
284
285         switch (ipalloc_state->algorithm) {
286         case IPALLOC_LCP2:
287                 ret = ipalloc_lcp2(ipalloc_state);
288                 break;
289         case IPALLOC_DETERMINISTIC:
290                 ret = ipalloc_deterministic(ipalloc_state);
291                 break;
292         case IPALLOC_NONDETERMINISTIC:
293                 ret = ipalloc_nondeterministic(ipalloc_state);
294                break;
295         }
296
297         /* at this point ->pnn is the node which will own each IP
298            or -1 if there is no node that can cover this ip
299         */
300
301         return (ret ? ipalloc_state->all_ips : NULL);
302 }