ctdb-recovery: Avoid -1 as a PNN, use CTDB_UNKNOWN_PNN instead
[amitay/samba.git] / ctdb / server / ipalloc_deterministic.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 "replace.h"
23 #include "system/network.h"
24
25 #include "lib/util/debug.h"
26 #include "common/logging.h"
27
28 #include "server/ipalloc_private.h"
29
30 bool ipalloc_deterministic(struct ipalloc_state *ipalloc_state)
31 {
32         struct public_ip_list *t;
33         int i, numnodes;
34
35         numnodes = ipalloc_state->num;
36
37         DEBUG(DEBUG_NOTICE,("Deterministic IPs enabled. Resetting all ip allocations\n"));
38        /* Allocate IPs to nodes in a modulo fashion so that IPs will
39         *  always be allocated the same way for a specific set of
40         *  available/unavailable nodes.
41         */
42
43         for (i = 0, t = ipalloc_state->all_ips; t!= NULL; t = t->next, i++) {
44                 t->pnn = i % numnodes;
45         }
46
47         /* IP failback doesn't make sense with deterministic
48          * IPs, since the modulo step above implicitly fails
49          * back IPs to their "home" node.
50          */
51         if (ipalloc_state->no_ip_failback) {
52                 D_WARNING("WARNING: 'NoIPFailback' set but ignored - "
53                           "incompatible with 'Deterministic IPs\n");
54         }
55
56         unassign_unsuitable_ips(ipalloc_state);
57
58         basic_allocate_unassigned(ipalloc_state);
59
60         /* No failback here! */
61
62         return true;
63 }