ctdb-ipalloc: Fix buggy short-circuit when no IPs are available
[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 <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 static void *add_ip_callback(void *parm, void *data)
35 {
36         struct public_ip_list *this_ip = parm;
37         struct public_ip_list *prev_ip = data;
38
39         if (prev_ip == NULL) {
40                 return parm;
41         }
42         if (this_ip->pnn == -1) {
43                 this_ip->pnn = prev_ip->pnn;
44         }
45
46         return parm;
47 }
48
49 static int getips_count_callback(void *param, void *data)
50 {
51         struct public_ip_list **ip_list = (struct public_ip_list **)param;
52         struct public_ip_list *new_ip = (struct public_ip_list *)data;
53
54         new_ip->next = *ip_list;
55         *ip_list     = new_ip;
56         return 0;
57 }
58
59 /* Nodes only know about those public addresses that they are
60  * configured to serve and no individual node has a full list of all
61  * public addresses configured across the cluster.  Therefore, a
62  * merged list of all public addresses needs to be built so that IP
63  * allocation can be done. */
64 static struct public_ip_list *
65 create_merged_ip_list(struct ipalloc_state *ipalloc_state,
66                       struct ctdb_public_ip_list *known_ips)
67 {
68         int i, j;
69         struct public_ip_list *ip_list;
70         struct ctdb_public_ip_list *public_ips;
71         struct trbt_tree *ip_tree;
72
73         ip_tree = trbt_create(ipalloc_state, 0);
74
75         if (known_ips == NULL) {
76                 DEBUG(DEBUG_ERR, ("Known public IPs not set\n"));
77                 return NULL;
78         }
79
80         for (i=0; i < ipalloc_state->num; i++) {
81
82                 public_ips = &known_ips[i];
83
84                 for (j=0; j < public_ips->num; j++) {
85                         struct public_ip_list *tmp_ip;
86
87                         /* This is returned as part of ip_list */
88                         tmp_ip = talloc_zero(ipalloc_state, struct public_ip_list);
89                         if (tmp_ip == NULL) {
90                                 DEBUG(DEBUG_ERR,
91                                       (__location__ " out of memory\n"));
92                                 talloc_free(ip_tree);
93                                 return NULL;
94                         }
95
96                         /* Do not use information about IP addresses hosted
97                          * on other nodes, it may not be accurate */
98                         if (public_ips->ip[j].pnn == i) {
99                                 tmp_ip->pnn = public_ips->ip[j].pnn;
100                         } else {
101                                 tmp_ip->pnn = -1;
102                         }
103                         tmp_ip->addr = public_ips->ip[j].addr;
104                         tmp_ip->next = NULL;
105
106                         trbt_insertarray32_callback(ip_tree,
107                                 IP_KEYLEN, ip_key(&public_ips->ip[j].addr),
108                                 add_ip_callback,
109                                 tmp_ip);
110                 }
111         }
112
113         ip_list = NULL;
114         trbt_traversearray32(ip_tree, IP_KEYLEN, getips_count_callback, &ip_list);
115         talloc_free(ip_tree);
116
117         return ip_list;
118 }
119
120 bool ipalloc_set_public_ips(struct ipalloc_state *ipalloc_state,
121                             struct ctdb_public_ip_list *known_ips,
122                             struct ctdb_public_ip_list *available_ips)
123 {
124         ipalloc_state->available_public_ips = available_ips;
125
126         ipalloc_state->all_ips = create_merged_ip_list(ipalloc_state,
127                                                        known_ips);
128
129         return (ipalloc_state->all_ips != NULL);
130 }
131
132 /* This can only return false if there are no available IPs *and*
133  * there are no IP addresses currently allocated.  If the latter is
134  * true then the cluster can clearly host IPs... just not necessarily
135  * right now... */
136 bool ipalloc_can_host_ips(struct ipalloc_state *ipalloc_state)
137 {
138         int i;
139         struct public_ip_list *ip_list;
140
141
142         for (ip_list = ipalloc_state->all_ips;
143              ip_list != NULL;
144              ip_list = ip_list->next) {
145                 if (ip_list->pnn != -1) {
146                         return true;
147                 }
148         }
149
150         for (i=0; i < ipalloc_state->num; i++) {
151                 if (ipalloc_state->available_public_ips[i].num != 0) {
152                         return true;
153                 }
154         }
155
156         return false;
157 }
158
159 /* The calculation part of the IP allocation algorithm. */
160 bool ipalloc(struct ipalloc_state *ipalloc_state)
161 {
162         bool ret = false;
163
164         switch (ipalloc_state->algorithm) {
165         case IPALLOC_LCP2:
166                 ret = ipalloc_lcp2(ipalloc_state);
167                 break;
168         case IPALLOC_DETERMINISTIC:
169                 ret = ipalloc_deterministic(ipalloc_state);
170                 break;
171         case IPALLOC_NONDETERMINISTIC:
172                 ret = ipalloc_nondeterministic(ipalloc_state);
173                break;
174         }
175
176         /* at this point ->pnn is the node which will own each IP
177            or -1 if there is no node that can cover this ip
178         */
179
180         return ret;
181 }