2 * net/tipc/cluster.c: TIPC cluster management routines
4 * Copyright (c) 2000-2006, Ericsson AB
5 * Copyright (c) 2005, Wind River Systems
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
20 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
41 static void tipc_cltr_multicast(struct cluster *c_ptr, struct sk_buff *buf,
42 u32 lower, u32 upper);
44 struct tipc_node **tipc_local_nodes = NULL;
45 struct tipc_node_map tipc_cltr_bcast_nodes = {0,{0,}};
47 struct cluster *tipc_cltr_create(u32 addr)
49 struct cluster *c_ptr;
52 c_ptr = kzalloc(sizeof(*c_ptr), GFP_ATOMIC);
54 warn("Cluster creation failure, no memory\n");
58 c_ptr->addr = tipc_addr(tipc_zone(addr), tipc_cluster(addr), 0);
59 max_nodes = tipc_max_nodes + 1;
61 c_ptr->nodes = kcalloc(max_nodes + 1, sizeof(void*), GFP_ATOMIC);
62 if (c_ptr->nodes == NULL) {
63 warn("Cluster creation failure, no memory for node area\n");
68 if (in_own_cluster(addr))
69 tipc_local_nodes = c_ptr->nodes;
70 c_ptr->highest_node = 0;
72 tipc_net.clusters[1] = c_ptr;
76 void tipc_cltr_delete(struct cluster *c_ptr)
82 for (n_num = 1; n_num <= c_ptr->highest_node; n_num++) {
83 tipc_node_delete(c_ptr->nodes[n_num]);
90 void tipc_cltr_attach_node(struct cluster *c_ptr, struct tipc_node *n_ptr)
92 u32 n_num = tipc_node(n_ptr->addr);
93 u32 max_n_num = tipc_max_nodes;
96 assert(n_num <= max_n_num);
97 assert(c_ptr->nodes[n_num] == NULL);
98 c_ptr->nodes[n_num] = n_ptr;
99 if (n_num > c_ptr->highest_node)
100 c_ptr->highest_node = n_num;
104 * tipc_cltr_select_router - select router to a cluster
106 * Uses deterministic and fair algorithm.
109 u32 tipc_cltr_select_router(struct cluster *c_ptr, u32 ref)
112 u32 ulim = c_ptr->highest_node;
116 assert(!in_own_cluster(c_ptr->addr));
120 /* Start entry must be random */
121 mask = tipc_max_nodes;
127 /* Lookup upwards with wrap-around */
129 if (tipc_node_is_up(c_ptr->nodes[n_num]))
131 } while (++n_num <= ulim);
135 if (tipc_node_is_up(c_ptr->nodes[n_num]))
137 } while (++n_num < tstart);
141 assert(n_num <= ulim);
142 return tipc_node_select_router(c_ptr->nodes[n_num], ref);
146 * tipc_cltr_select_node - select destination node within a remote cluster
148 * Uses deterministic and fair algorithm.
151 struct tipc_node *tipc_cltr_select_node(struct cluster *c_ptr, u32 selector)
154 u32 mask = tipc_max_nodes;
157 assert(!in_own_cluster(c_ptr->addr));
158 if (!c_ptr->highest_node)
161 /* Start entry must be random */
162 while (mask > c_ptr->highest_node) {
165 start_entry = (selector & mask) ? selector & mask : 1u;
166 assert(start_entry <= c_ptr->highest_node);
168 /* Lookup upwards with wrap-around */
169 for (n_num = start_entry; n_num <= c_ptr->highest_node; n_num++) {
170 if (tipc_node_has_active_links(c_ptr->nodes[n_num]))
171 return c_ptr->nodes[n_num];
173 for (n_num = 1; n_num < start_entry; n_num++) {
174 if (tipc_node_has_active_links(c_ptr->nodes[n_num]))
175 return c_ptr->nodes[n_num];
181 * Routing table management: See description in node.c
184 static struct sk_buff *tipc_cltr_prepare_routing_msg(u32 data_size, u32 dest)
186 u32 size = INT_H_SIZE + data_size;
187 struct sk_buff *buf = tipc_buf_acquire(size);
188 struct tipc_msg *msg;
192 memset((char *)msg, 0, size);
193 tipc_msg_init(msg, ROUTE_DISTRIBUTOR, 0, INT_H_SIZE, dest);
198 void tipc_cltr_bcast_new_route(struct cluster *c_ptr, u32 dest,
199 u32 lower, u32 upper)
201 struct sk_buff *buf = tipc_cltr_prepare_routing_msg(0, c_ptr->addr);
202 struct tipc_msg *msg;
206 msg_set_remote_node(msg, dest);
207 msg_set_type(msg, ROUTE_ADDITION);
208 tipc_cltr_multicast(c_ptr, buf, lower, upper);
210 warn("Memory squeeze: broadcast of new route failed\n");
214 void tipc_cltr_bcast_lost_route(struct cluster *c_ptr, u32 dest,
215 u32 lower, u32 upper)
217 struct sk_buff *buf = tipc_cltr_prepare_routing_msg(0, c_ptr->addr);
218 struct tipc_msg *msg;
222 msg_set_remote_node(msg, dest);
223 msg_set_type(msg, ROUTE_REMOVAL);
224 tipc_cltr_multicast(c_ptr, buf, lower, upper);
226 warn("Memory squeeze: broadcast of lost route failed\n");
230 void tipc_cltr_send_ext_routes(struct cluster *c_ptr, u32 dest)
233 struct tipc_msg *msg;
234 u32 highest = c_ptr->highest_node;
238 if (in_own_cluster(c_ptr->addr))
240 assert(in_own_cluster(dest));
241 highest = c_ptr->highest_node;
242 buf = tipc_cltr_prepare_routing_msg(highest + 1, c_ptr->addr);
245 msg_set_remote_node(msg, c_ptr->addr);
246 msg_set_type(msg, EXT_ROUTING_TABLE);
247 for (n_num = 1; n_num <= highest; n_num++) {
248 if (c_ptr->nodes[n_num] &&
249 tipc_node_has_active_links(c_ptr->nodes[n_num])) {
251 msg_set_dataoctet(msg, n_num);
255 tipc_link_send(buf, dest, dest);
259 warn("Memory squeeze: broadcast of external route failed\n");
263 void tipc_cltr_recv_routing_table(struct sk_buff *buf)
265 struct tipc_msg *msg = buf_msg(buf);
266 struct cluster *c_ptr;
267 struct tipc_node *n_ptr;
271 u32 rem_node = msg_remote_node(msg);
276 c_ptr = tipc_cltr_find(rem_node);
278 c_ptr = tipc_cltr_create(rem_node);
285 node_table = buf->data + msg_hdr_sz(msg);
286 table_size = msg_size(msg) - msg_hdr_sz(msg);
287 router = msg_prevnode(msg);
288 z_num = tipc_zone(rem_node);
289 c_num = tipc_cluster(rem_node);
291 switch (msg_type(msg)) {
292 case EXT_ROUTING_TABLE:
293 for (n_num = 1; n_num < table_size; n_num++) {
294 if (node_table[n_num]) {
295 u32 addr = tipc_addr(z_num, c_num, n_num);
296 n_ptr = c_ptr->nodes[n_num];
298 n_ptr = tipc_node_create(addr);
301 tipc_node_add_router(n_ptr, router);
305 case SLAVE_ROUTING_TABLE:
306 assert(in_own_cluster(c_ptr->addr));
309 assert(!in_own_cluster(c_ptr->addr));
310 n_ptr = c_ptr->nodes[tipc_node(rem_node)];
312 n_ptr = tipc_node_create(rem_node);
314 tipc_node_add_router(n_ptr, router);
317 assert(!in_own_cluster(c_ptr->addr));
318 n_ptr = c_ptr->nodes[tipc_node(rem_node)];
320 tipc_node_remove_router(n_ptr, router);
323 assert(!"Illegal routing manager message received\n");
328 void tipc_cltr_remove_as_router(struct cluster *c_ptr, u32 router)
332 if (in_own_cluster(c_ptr->addr))
335 for (n_num = 1; n_num <= c_ptr->highest_node; n_num++) {
336 if (c_ptr->nodes[n_num]) {
337 tipc_node_remove_router(c_ptr->nodes[n_num], router);
343 * tipc_cltr_multicast - multicast message to local nodes
346 static void tipc_cltr_multicast(struct cluster *c_ptr, struct sk_buff *buf,
347 u32 lower, u32 upper)
349 struct sk_buff *buf_copy;
350 struct tipc_node *n_ptr;
354 assert(lower <= upper);
355 assert((lower >= 1) && (lower <= tipc_max_nodes));
356 assert((upper >= 1) && (upper <= tipc_max_nodes));
357 assert(in_own_cluster(c_ptr->addr));
359 tstop = c_ptr->highest_node;
362 for (n_num = lower; n_num <= tstop; n_num++) {
363 n_ptr = c_ptr->nodes[n_num];
364 if (n_ptr && tipc_node_has_active_links(n_ptr)) {
365 buf_copy = skb_copy(buf, GFP_ATOMIC);
366 if (buf_copy == NULL)
368 msg_set_destnode(buf_msg(buf_copy), n_ptr->addr);
369 tipc_link_send(buf_copy, n_ptr->addr, n_ptr->addr);
376 * tipc_cltr_broadcast - broadcast message to all nodes within cluster
379 void tipc_cltr_broadcast(struct sk_buff *buf)
381 struct sk_buff *buf_copy;
382 struct cluster *c_ptr;
383 struct tipc_node *n_ptr;
386 if (tipc_mode == TIPC_NET_MODE) {
387 c_ptr = tipc_cltr_find(tipc_own_addr);
388 assert(in_own_cluster(c_ptr->addr)); /* For now */
391 for (n_num = 1; n_num <= c_ptr->highest_node; n_num++) {
392 n_ptr = c_ptr->nodes[n_num];
393 if (n_ptr && tipc_node_has_active_links(n_ptr)) {
394 buf_copy = skb_copy(buf, GFP_ATOMIC);
395 if (buf_copy == NULL)
397 msg_set_destnode(buf_msg(buf_copy),
399 tipc_link_send(buf_copy, n_ptr->addr,
408 int tipc_cltr_init(void)
410 return tipc_cltr_create(tipc_own_addr) ? 0 : -ENOMEM;