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/>.
23 #include "system/network.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
,
40 enum ipalloc_algorithm algorithm
,
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"));
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
;
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
) {
70 if (this_ip
->pnn
== CTDB_UNKNOWN_PNN
) {
71 this_ip
->pnn
= prev_ip
->pnn
;
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
;
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
)
96 struct public_ip_list
*ip_list
;
97 struct ctdb_public_ip_list
*public_ips
;
98 struct trbt_tree
*ip_tree
;
101 ip_tree
= trbt_create(ipalloc_state
, 0);
103 if (ipalloc_state
->known_public_ips
== NULL
) {
104 DEBUG(DEBUG_ERR
, ("Known public IPs not set\n"));
108 for (i
=0; i
< ipalloc_state
->num
; i
++) {
110 public_ips
= &ipalloc_state
->known_public_ips
[i
];
112 for (j
=0; j
< public_ips
->num
; j
++) {
113 struct public_ip_list
*tmp_ip
;
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
) {
119 (__location__
" out of memory\n"));
120 talloc_free(ip_tree
);
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
;
129 tmp_ip
->pnn
= CTDB_UNKNOWN_PNN
;
131 tmp_ip
->addr
= public_ips
->ip
[j
].addr
;
134 trbt_insertarray32_callback(ip_tree
,
135 IP_KEYLEN
, ip_key(&public_ips
->ip
[j
].addr
),
142 ret
= trbt_traversearray32(ip_tree
, IP_KEYLEN
, getips_count_callback
, &ip_list
);
144 DBG_ERR("Error traversing the IP tree.\n");
147 talloc_free(ip_tree
);
152 static bool populate_bitmap(struct ipalloc_state
*ipalloc_state
)
154 struct public_ip_list
*ip
= NULL
;
157 for (ip
= ipalloc_state
->all_ips
; ip
!= NULL
; ip
= ip
->next
) {
159 ip
->known_on
= bitmap_talloc(ip
, ipalloc_state
->num
);
160 if (ip
->known_on
== NULL
) {
164 ip
->available_on
= bitmap_talloc(ip
, ipalloc_state
->num
);
165 if (ip
->available_on
== NULL
) {
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
];
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
);
184 /* Optimisation: available => known */
185 if (bitmap_query(ip
->available_on
, i
)) {
186 bitmap_set(ip
->known_on
, i
);
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
);
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
)
208 ipalloc_state
->available_public_ips
= available_ips
;
209 ipalloc_state
->known_public_ips
= known_ips
;
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
216 bool ipalloc_can_host_ips(struct ipalloc_state
*ipalloc_state
)
219 bool have_ips
= false;
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) {
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
) {
240 /* At this point there are known addresses but none are
241 * hosted. Need to check if cluster can now host some
244 for (i
=0; i
< ipalloc_state
->num
; i
++) {
245 if (ipalloc_state
->available_public_ips
[i
].num
!= 0) {
253 /* The calculation part of the IP allocation algorithm. */
254 struct public_ip_list
*ipalloc(struct ipalloc_state
*ipalloc_state
)
258 ipalloc_state
->all_ips
= create_merged_ip_list(ipalloc_state
);
259 if (ipalloc_state
->all_ips
== NULL
) {
263 if (!populate_bitmap(ipalloc_state
)) {
267 switch (ipalloc_state
->algorithm
) {
269 ret
= ipalloc_lcp2(ipalloc_state
);
271 case IPALLOC_DETERMINISTIC
:
272 ret
= ipalloc_deterministic(ipalloc_state
);
274 case IPALLOC_NONDETERMINISTIC
:
275 ret
= ipalloc_nondeterministic(ipalloc_state
);
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
283 return (ret
? ipalloc_state
->all_ips
: NULL
);