summaryrefslogtreecommitdiffstats
path: root/ctdb/server/ipalloc_deterministic.c
blob: 43680ba5c2fcde87f36d200d6a59ec7c042627b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
   ctdb ip takeover code

   Copyright (C) Ronnie Sahlberg  2007
   Copyright (C) Andrew Tridgell  2007
   Copyright (C) Martin Schwenke  2011

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, see <http://www.gnu.org/licenses/>.
*/

#include "replace.h"
#include "system/network.h"

#include "lib/util/debug.h"
#include "common/logging.h"
#include "common/path.h"

#include "protocol/protocol_util.h"
#include "lib/util/smb_strtox.h"
#include "lib/util/memory.h"

#include "server/ipalloc_private.h"

struct home_node {
	ctdb_sock_addr addr;
	uint32_t pnn;
};

static struct home_node *ipalloc_get_home_nodes(TALLOC_CTX *mem_ctx)
{
	char *line = NULL;
	size_t len = 0;
	char *fname = NULL;
	FILE *fp = NULL;
	struct home_node *result = NULL;

	fname = path_etcdir_append(mem_ctx, "home_nodes");
	if (fname == NULL) {
		goto fail;
	}

	fp = fopen(fname, "r");
	if (fp == NULL) {
		goto fail;
	}
	TALLOC_FREE(fname);

	while (true) {
		size_t num_nodes = talloc_array_length(result);
		char *saveptr = NULL, *addrstr = NULL, *nodestr = NULL;
		struct home_node hn = {
			.pnn = CTDB_UNKNOWN_PNN,
		};
		struct home_node *tmp = NULL;
		ssize_t n = 0;
		int ret;

		n = getline(&line, &len, fp);
		if (n < 0) {
			if (!feof(fp)) {
				/* real error */
				goto fail;
			}
			break;
		}
		if ((n > 0) && (line[n - 1] == '\n')) {
			line[n - 1] = '\0';
		}

		addrstr = strtok_r(line, " \t", &saveptr);
		if (addrstr == NULL) {
			continue;
		}
		nodestr = strtok_r(NULL, " \t", &saveptr);
		if (nodestr == NULL) {
			continue;
		}

		ret = ctdb_sock_addr_from_string(addrstr, &hn.addr, false);
		if (ret != 0) {
			DBG_WARNING("Could not parse %s: %s\n",
				    addrstr,
				    strerror(ret));
			goto fail;
		}

		hn.pnn = smb_strtoul(nodestr,
				     NULL,
				     10,
				     &ret,
				     SMB_STR_FULL_STR_CONV);
		if (ret != 0) {
			DBG_WARNING("Could not parse \"%s\"\n", nodestr);
			goto fail;
		}

		tmp = talloc_realloc(mem_ctx,
				     result,
				     struct home_node,
				     num_nodes + 1);
		if (tmp == NULL) {
			goto fail;
		}
		result = tmp;
		result[num_nodes] = hn;
	}

	fclose(fp);
	fp = NULL;
	return result;

fail:
	if (fp != NULL) {
		fclose(fp);
		fp = NULL;
	}
	SAFE_FREE(line);
	TALLOC_FREE(fname);
	TALLOC_FREE(result);
	return NULL;
}

bool ipalloc_deterministic(struct ipalloc_state *ipalloc_state)
{
	struct home_node *home_nodes = ipalloc_get_home_nodes(ipalloc_state);
	size_t num_home_nodes = talloc_array_length(home_nodes);
	struct public_ip_list *t;
	int i;
	uint32_t numnodes;

	numnodes = ipalloc_state->num;

	DEBUG(DEBUG_NOTICE,("Deterministic IPs enabled. Resetting all ip allocations\n"));
       /* Allocate IPs to nodes in a modulo fashion so that IPs will
        *  always be allocated the same way for a specific set of
        *  available/unavailable nodes.
	*/

	for (i = 0, t = ipalloc_state->all_ips; t!= NULL; t = t->next, i++) {
		size_t j;

		t->pnn = i % numnodes;

		for (j = 0; j < num_home_nodes; j++) {
			struct home_node *hn = &home_nodes[j];

			if (ctdb_sock_addr_same_ip(&t->addr, &hn->addr)) {

				if (hn->pnn >= numnodes) {
					DBG_WARNING("pnn %" PRIu32
						    " too large\n",
						    hn->pnn);
					break;
				}

				t->pnn = hn->pnn;
				break;
			}
		}
	}

	/* IP failback doesn't make sense with deterministic
	 * IPs, since the modulo step above implicitly fails
	 * back IPs to their "home" node.
	 */
	if (ipalloc_state->no_ip_failback) {
		D_WARNING("WARNING: 'NoIPFailback' set but ignored - "
			  "incompatible with 'Deterministic IPs\n");
	}

	unassign_unsuitable_ips(ipalloc_state);

	basic_allocate_unassigned(ipalloc_state);

	/* No failback here! */

	TALLOC_FREE(home_nodes);

	return true;
}