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
== -1) {
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
;
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"));
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
) {
118 (__location__
" out of memory\n"));
119 talloc_free(ip_tree
);
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
;
130 tmp_ip
->addr
= public_ips
->ip
[j
].addr
;
133 trbt_insertarray32_callback(ip_tree
,
134 IP_KEYLEN
, ip_key(&public_ips
->ip
[j
].addr
),
141 trbt_traversearray32(ip_tree
, IP_KEYLEN
, getips_count_callback
, &ip_list
);
142 talloc_free(ip_tree
);
147 static bool populate_bitmap(struct ipalloc_state
*ipalloc_state
)
149 struct public_ip_list
*ip
= NULL
;
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
) {
159 ip
->available_on
= bitmap_talloc(ip
, ipalloc_state
->num
);
160 if (ip
->available_on
== NULL
) {
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
);
179 /* Optimisation: available => known */
180 if (bitmap_query(ip
->available_on
, i
)) {
181 bitmap_set(ip
->known_on
, i
);
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
);
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
211 bool ipalloc_can_host_ips(struct ipalloc_state
*ipalloc_state
)
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) {
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
) {
235 /* At this point there are known addresses but none are
236 * hosted. Need to check if cluster can now host some
239 for (i
=0; i
< ipalloc_state
->num
; i
++) {
240 if (ipalloc_state
->available_public_ips
[i
].num
!= 0) {
248 /* The calculation part of the IP allocation algorithm. */
249 struct public_ip_list
*ipalloc(struct ipalloc_state
*ipalloc_state
)
253 ipalloc_state
->all_ips
= create_merged_ip_list(ipalloc_state
);
254 if (ipalloc_state
->all_ips
== NULL
) {
258 if (!populate_bitmap(ipalloc_state
)) {
262 switch (ipalloc_state
->algorithm
) {
264 ret
= ipalloc_lcp2(ipalloc_state
);
266 case IPALLOC_DETERMINISTIC
:
267 ret
= ipalloc_deterministic(ipalloc_state
);
269 case IPALLOC_NONDETERMINISTIC
:
270 ret
= ipalloc_nondeterministic(ipalloc_state
);
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
);