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/>.
25 #include "system/network.h"
27 #include "lib/util/debug.h"
29 #include "common/logging.h"
30 #include "common/rb_tree.h"
32 #include "server/ipalloc_private.h"
34 static void *add_ip_callback(void *parm, void *data)
36 struct public_ip_list *this_ip = parm;
37 struct public_ip_list *prev_ip = data;
39 if (prev_ip == NULL) {
42 if (this_ip->pnn == -1) {
43 this_ip->pnn = prev_ip->pnn;
49 static int getips_count_callback(void *param, void *data)
51 struct public_ip_list **ip_list = (struct public_ip_list **)param;
52 struct public_ip_list *new_ip = (struct public_ip_list *)data;
54 new_ip->next = *ip_list;
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)
68 struct public_ip_list *ip_list;
69 struct ctdb_public_ip_list *public_ips;
70 struct trbt_tree *ip_tree;
72 ip_tree = trbt_create(ipalloc_state, 0);
74 if (ipalloc_state->known_public_ips == NULL) {
75 DEBUG(DEBUG_ERR, ("Known public IPs not set\n"));
79 for (i=0; i < ipalloc_state->num; i++) {
81 public_ips = &ipalloc_state->known_public_ips[i];
83 for (j=0; j < public_ips->num; j++) {
84 struct public_ip_list *tmp_ip;
86 /* This is returned as part of ip_list */
87 tmp_ip = talloc_zero(ipalloc_state, struct public_ip_list);
90 (__location__ " out of memory\n"));
95 /* Do not use information about IP addresses hosted
96 * on other nodes, it may not be accurate */
97 if (public_ips->ip[j].pnn == i) {
98 tmp_ip->pnn = public_ips->ip[j].pnn;
102 tmp_ip->addr = public_ips->ip[j].addr;
105 trbt_insertarray32_callback(ip_tree,
106 IP_KEYLEN, ip_key(&public_ips->ip[j].addr),
113 trbt_traversearray32(ip_tree, IP_KEYLEN, getips_count_callback, &ip_list);
114 talloc_free(ip_tree);
119 bool ipalloc_set_public_ips(struct ipalloc_state *ipalloc_state,
120 struct ctdb_public_ip_list *known_ips,
121 struct ctdb_public_ip_list *available_ips)
123 ipalloc_state->known_public_ips = known_ips;
124 ipalloc_state->available_public_ips = available_ips;
126 ipalloc_state->all_ips = create_merged_ip_list(ipalloc_state);
128 return (ipalloc_state->all_ips != NULL);
131 /* The calculation part of the IP allocation algorithm. */
132 bool ipalloc(struct ipalloc_state *ipalloc_state)
136 switch (ipalloc_state->algorithm) {
138 ret = ipalloc_lcp2(ipalloc_state);
140 case IPALLOC_DETERMINISTIC:
141 ret = ipalloc_deterministic(ipalloc_state);
143 case IPALLOC_NONDETERMINISTIC:
144 ret = ipalloc_nondeterministic(ipalloc_state);
148 /* at this point ->pnn is the node which will own each IP
149 or -1 if there is no node that can cover this ip