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"
25 #include "lib/util/debug.h"
26 #include "common/logging.h"
27 #include "common/path.h"
29 #include "protocol/protocol_util.h"
30 #include "lib/util/smb_strtox.h"
31 #include "lib/util/memory.h"
33 #include "server/ipalloc_private.h"
40 static struct home_node
*ipalloc_get_home_nodes(TALLOC_CTX
*mem_ctx
)
46 struct home_node
*result
= NULL
;
48 fname
= path_etcdir_append(mem_ctx
, "home_nodes");
53 fp
= fopen(fname
, "r");
60 size_t num_nodes
= talloc_array_length(result
);
61 char *saveptr
= NULL
, *addrstr
= NULL
, *nodestr
= NULL
;
62 struct home_node hn
= {
63 .pnn
= CTDB_UNKNOWN_PNN
,
65 struct home_node
*tmp
= NULL
;
69 n
= getline(&line
, &len
, fp
);
77 if ((n
> 0) && (line
[n
- 1] == '\n')) {
81 addrstr
= strtok_r(line
, " \t", &saveptr
);
82 if (addrstr
== NULL
) {
85 nodestr
= strtok_r(NULL
, " \t", &saveptr
);
86 if (nodestr
== NULL
) {
90 ret
= ctdb_sock_addr_from_string(addrstr
, &hn
.addr
, false);
92 DBG_WARNING("Could not parse %s: %s\n",
98 hn
.pnn
= smb_strtoul(nodestr
,
102 SMB_STR_FULL_STR_CONV
);
104 DBG_WARNING("Could not parse \"%s\"\n", nodestr
);
108 tmp
= talloc_realloc(mem_ctx
,
116 result
[num_nodes
] = hn
;
134 bool ipalloc_deterministic(struct ipalloc_state
*ipalloc_state
)
136 struct home_node
*home_nodes
= ipalloc_get_home_nodes(ipalloc_state
);
137 size_t num_home_nodes
= talloc_array_length(home_nodes
);
138 struct public_ip_list
*t
;
142 numnodes
= ipalloc_state
->num
;
144 DEBUG(DEBUG_NOTICE
,("Deterministic IPs enabled. Resetting all ip allocations\n"));
145 /* Allocate IPs to nodes in a modulo fashion so that IPs will
146 * always be allocated the same way for a specific set of
147 * available/unavailable nodes.
150 for (i
= 0, t
= ipalloc_state
->all_ips
; t
!= NULL
; t
= t
->next
, i
++) {
153 t
->pnn
= i
% numnodes
;
155 for (j
= 0; j
< num_home_nodes
; j
++) {
156 struct home_node
*hn
= &home_nodes
[j
];
158 if (ctdb_sock_addr_same_ip(&t
->addr
, &hn
->addr
)) {
160 if (hn
->pnn
>= numnodes
) {
161 DBG_WARNING("pnn %" PRIu32
173 /* IP failback doesn't make sense with deterministic
174 * IPs, since the modulo step above implicitly fails
175 * back IPs to their "home" node.
177 if (ipalloc_state
->no_ip_failback
) {
178 D_WARNING("WARNING: 'NoIPFailback' set but ignored - "
179 "incompatible with 'Deterministic IPs\n");
182 unassign_unsuitable_ips(ipalloc_state
);
184 basic_allocate_unassigned(ipalloc_state
);
186 /* No failback here! */
188 TALLOC_FREE(home_nodes
);