2 * net/tipc/cluster.c: TIPC cluster management routines
4 * Copyright (c) 2003-2005, Ericsson Research Canada
5 * Copyright (c) 2005, Wind River Systems
6 * Copyright (c) 2005-2006, Ericsson AB
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the names of the copyright holders nor the names of its
18 * contributors may be used to endorse or promote products derived from
19 * this software without specific prior written permission.
21 * Alternatively, this software may be distributed under the terms of the
22 * GNU General Public License ("GPL") version 2 as published by the Free
23 * Software Foundation.
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
29 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
41 #include "node_subscr.h"
48 void cluster_multicast(struct cluster *c_ptr, struct sk_buff *buf,
49 u32 lower, u32 upper);
50 struct sk_buff *cluster_prepare_routing_msg(u32 data_size, u32 dest);
52 struct node **local_nodes = 0;
53 struct node_map cluster_bcast_nodes = {0,{0,}};
54 u32 highest_allowed_slave = 0;
56 struct cluster *cluster_create(u32 addr)
59 struct cluster *c_ptr;
63 c_ptr = (struct cluster *)kmalloc(sizeof(*c_ptr), GFP_ATOMIC);
66 memset(c_ptr, 0, sizeof(*c_ptr));
68 c_ptr->addr = tipc_addr(tipc_zone(addr), tipc_cluster(addr), 0);
69 if (in_own_cluster(addr))
70 max_nodes = LOWEST_SLAVE + tipc_max_slaves;
72 max_nodes = tipc_max_nodes + 1;
73 alloc = sizeof(void *) * (max_nodes + 1);
74 c_ptr->nodes = (struct node **)kmalloc(alloc, GFP_ATOMIC);
75 if (c_ptr->nodes == NULL) {
79 memset(c_ptr->nodes, 0, alloc);
80 if (in_own_cluster(addr))
81 local_nodes = c_ptr->nodes;
82 c_ptr->highest_slave = LOWEST_SLAVE - 1;
83 c_ptr->highest_node = 0;
85 z_ptr = zone_find(tipc_zone(addr));
87 z_ptr = zone_create(addr);
90 zone_attach_cluster(z_ptr, c_ptr);
101 void cluster_delete(struct cluster *c_ptr)
107 for (n_num = 1; n_num <= c_ptr->highest_node; n_num++) {
108 node_delete(c_ptr->nodes[n_num]);
110 for (n_num = LOWEST_SLAVE; n_num <= c_ptr->highest_slave; n_num++) {
111 node_delete(c_ptr->nodes[n_num]);
117 u32 cluster_next_node(struct cluster *c_ptr, u32 addr)
120 u32 n_num = tipc_node(addr) + 1;
124 for (; n_num <= c_ptr->highest_node; n_num++) {
125 n_ptr = c_ptr->nodes[n_num];
126 if (n_ptr && node_has_active_links(n_ptr))
129 for (n_num = 1; n_num < tipc_node(addr); n_num++) {
130 n_ptr = c_ptr->nodes[n_num];
131 if (n_ptr && node_has_active_links(n_ptr))
137 void cluster_attach_node(struct cluster *c_ptr, struct node *n_ptr)
139 u32 n_num = tipc_node(n_ptr->addr);
140 u32 max_n_num = tipc_max_nodes;
142 if (in_own_cluster(n_ptr->addr))
143 max_n_num = highest_allowed_slave;
145 assert(n_num <= max_n_num);
146 assert(c_ptr->nodes[n_num] == 0);
147 c_ptr->nodes[n_num] = n_ptr;
148 if (n_num > c_ptr->highest_node)
149 c_ptr->highest_node = n_num;
153 * cluster_select_router - select router to a cluster
155 * Uses deterministic and fair algorithm.
158 u32 cluster_select_router(struct cluster *c_ptr, u32 ref)
161 u32 ulim = c_ptr->highest_node;
165 assert(!in_own_cluster(c_ptr->addr));
169 /* Start entry must be random */
170 mask = tipc_max_nodes;
176 /* Lookup upwards with wrap-around */
178 if (node_is_up(c_ptr->nodes[n_num]))
180 } while (++n_num <= ulim);
184 if (node_is_up(c_ptr->nodes[n_num]))
186 } while (++n_num < tstart);
190 assert(n_num <= ulim);
191 return node_select_router(c_ptr->nodes[n_num], ref);
195 * cluster_select_node - select destination node within a remote cluster
197 * Uses deterministic and fair algorithm.
200 struct node *cluster_select_node(struct cluster *c_ptr, u32 selector)
203 u32 mask = tipc_max_nodes;
206 assert(!in_own_cluster(c_ptr->addr));
207 if (!c_ptr->highest_node)
210 /* Start entry must be random */
211 while (mask > c_ptr->highest_node) {
214 start_entry = (selector & mask) ? selector & mask : 1u;
215 assert(start_entry <= c_ptr->highest_node);
217 /* Lookup upwards with wrap-around */
218 for (n_num = start_entry; n_num <= c_ptr->highest_node; n_num++) {
219 if (node_has_active_links(c_ptr->nodes[n_num]))
220 return c_ptr->nodes[n_num];
222 for (n_num = 1; n_num < start_entry; n_num++) {
223 if (node_has_active_links(c_ptr->nodes[n_num]))
224 return c_ptr->nodes[n_num];
230 * Routing table management: See description in node.c
233 struct sk_buff *cluster_prepare_routing_msg(u32 data_size, u32 dest)
235 u32 size = INT_H_SIZE + data_size;
236 struct sk_buff *buf = buf_acquire(size);
237 struct tipc_msg *msg;
241 memset((char *)msg, 0, size);
242 msg_init(msg, ROUTE_DISTRIBUTOR, 0, TIPC_OK, INT_H_SIZE, dest);
247 void cluster_bcast_new_route(struct cluster *c_ptr, u32 dest,
248 u32 lower, u32 upper)
250 struct sk_buff *buf = cluster_prepare_routing_msg(0, c_ptr->addr);
251 struct tipc_msg *msg;
255 msg_set_remote_node(msg, dest);
256 msg_set_type(msg, ROUTE_ADDITION);
257 cluster_multicast(c_ptr, buf, lower, upper);
259 warn("Memory squeeze: broadcast of new route failed\n");
263 void cluster_bcast_lost_route(struct cluster *c_ptr, u32 dest,
264 u32 lower, u32 upper)
266 struct sk_buff *buf = cluster_prepare_routing_msg(0, c_ptr->addr);
267 struct tipc_msg *msg;
271 msg_set_remote_node(msg, dest);
272 msg_set_type(msg, ROUTE_REMOVAL);
273 cluster_multicast(c_ptr, buf, lower, upper);
275 warn("Memory squeeze: broadcast of lost route failed\n");
279 void cluster_send_slave_routes(struct cluster *c_ptr, u32 dest)
282 struct tipc_msg *msg;
283 u32 highest = c_ptr->highest_slave;
287 assert(!is_slave(dest));
288 assert(in_own_cluster(dest));
289 assert(in_own_cluster(c_ptr->addr));
290 if (highest <= LOWEST_SLAVE)
292 buf = cluster_prepare_routing_msg(highest - LOWEST_SLAVE + 1,
296 msg_set_remote_node(msg, c_ptr->addr);
297 msg_set_type(msg, SLAVE_ROUTING_TABLE);
298 for (n_num = LOWEST_SLAVE; n_num <= highest; n_num++) {
299 if (c_ptr->nodes[n_num] &&
300 node_has_active_links(c_ptr->nodes[n_num])) {
302 msg_set_dataoctet(msg, n_num);
306 link_send(buf, dest, dest);
310 warn("Memory squeeze: broadcast of lost route failed\n");
314 void cluster_send_ext_routes(struct cluster *c_ptr, u32 dest)
317 struct tipc_msg *msg;
318 u32 highest = c_ptr->highest_node;
322 if (in_own_cluster(c_ptr->addr))
324 assert(!is_slave(dest));
325 assert(in_own_cluster(dest));
326 highest = c_ptr->highest_node;
327 buf = cluster_prepare_routing_msg(highest + 1, c_ptr->addr);
330 msg_set_remote_node(msg, c_ptr->addr);
331 msg_set_type(msg, EXT_ROUTING_TABLE);
332 for (n_num = 1; n_num <= highest; n_num++) {
333 if (c_ptr->nodes[n_num] &&
334 node_has_active_links(c_ptr->nodes[n_num])) {
336 msg_set_dataoctet(msg, n_num);
340 link_send(buf, dest, dest);
344 warn("Memory squeeze: broadcast of external route failed\n");
348 void cluster_send_local_routes(struct cluster *c_ptr, u32 dest)
351 struct tipc_msg *msg;
352 u32 highest = c_ptr->highest_node;
356 assert(is_slave(dest));
357 assert(in_own_cluster(c_ptr->addr));
358 buf = cluster_prepare_routing_msg(highest, c_ptr->addr);
361 msg_set_remote_node(msg, c_ptr->addr);
362 msg_set_type(msg, LOCAL_ROUTING_TABLE);
363 for (n_num = 1; n_num <= highest; n_num++) {
364 if (c_ptr->nodes[n_num] &&
365 node_has_active_links(c_ptr->nodes[n_num])) {
367 msg_set_dataoctet(msg, n_num);
371 link_send(buf, dest, dest);
375 warn("Memory squeeze: broadcast of local route failed\n");
379 void cluster_recv_routing_table(struct sk_buff *buf)
381 struct tipc_msg *msg = buf_msg(buf);
382 struct cluster *c_ptr;
387 u32 rem_node = msg_remote_node(msg);
392 c_ptr = cluster_find(rem_node);
394 c_ptr = cluster_create(rem_node);
401 node_table = buf->data + msg_hdr_sz(msg);
402 table_size = msg_size(msg) - msg_hdr_sz(msg);
403 router = msg_prevnode(msg);
404 z_num = tipc_zone(rem_node);
405 c_num = tipc_cluster(rem_node);
407 switch (msg_type(msg)) {
408 case LOCAL_ROUTING_TABLE:
409 assert(is_slave(tipc_own_addr));
410 case EXT_ROUTING_TABLE:
411 for (n_num = 1; n_num < table_size; n_num++) {
412 if (node_table[n_num]) {
413 u32 addr = tipc_addr(z_num, c_num, n_num);
414 n_ptr = c_ptr->nodes[n_num];
416 n_ptr = node_create(addr);
419 node_add_router(n_ptr, router);
423 case SLAVE_ROUTING_TABLE:
424 assert(!is_slave(tipc_own_addr));
425 assert(in_own_cluster(c_ptr->addr));
426 for (n_num = 1; n_num < table_size; n_num++) {
427 if (node_table[n_num]) {
428 u32 slave_num = n_num + LOWEST_SLAVE;
429 u32 addr = tipc_addr(z_num, c_num, slave_num);
430 n_ptr = c_ptr->nodes[slave_num];
432 n_ptr = node_create(addr);
435 node_add_router(n_ptr, router);
440 if (!is_slave(tipc_own_addr)) {
441 assert(!in_own_cluster(c_ptr->addr)
442 || is_slave(rem_node));
444 assert(in_own_cluster(c_ptr->addr)
445 && !is_slave(rem_node));
447 n_ptr = c_ptr->nodes[tipc_node(rem_node)];
449 n_ptr = node_create(rem_node);
451 node_add_router(n_ptr, router);
454 if (!is_slave(tipc_own_addr)) {
455 assert(!in_own_cluster(c_ptr->addr)
456 || is_slave(rem_node));
458 assert(in_own_cluster(c_ptr->addr)
459 && !is_slave(rem_node));
461 n_ptr = c_ptr->nodes[tipc_node(rem_node)];
463 node_remove_router(n_ptr, router);
466 assert(!"Illegal routing manager message received\n");
471 void cluster_remove_as_router(struct cluster *c_ptr, u32 router)
477 if (is_slave(router))
478 return; /* Slave nodes can not be routers */
480 if (in_own_cluster(c_ptr->addr)) {
481 start_entry = LOWEST_SLAVE;
482 tstop = c_ptr->highest_slave;
485 tstop = c_ptr->highest_node;
488 for (n_num = start_entry; n_num <= tstop; n_num++) {
489 if (c_ptr->nodes[n_num]) {
490 node_remove_router(c_ptr->nodes[n_num], router);
496 * cluster_multicast - multicast message to local nodes
499 void cluster_multicast(struct cluster *c_ptr, struct sk_buff *buf,
500 u32 lower, u32 upper)
502 struct sk_buff *buf_copy;
507 assert(lower <= upper);
508 assert(((lower >= 1) && (lower <= tipc_max_nodes)) ||
509 ((lower >= LOWEST_SLAVE) && (lower <= highest_allowed_slave)));
510 assert(((upper >= 1) && (upper <= tipc_max_nodes)) ||
511 ((upper >= LOWEST_SLAVE) && (upper <= highest_allowed_slave)));
512 assert(in_own_cluster(c_ptr->addr));
514 tstop = is_slave(upper) ? c_ptr->highest_slave : c_ptr->highest_node;
517 for (n_num = lower; n_num <= tstop; n_num++) {
518 n_ptr = c_ptr->nodes[n_num];
519 if (n_ptr && node_has_active_links(n_ptr)) {
520 buf_copy = skb_copy(buf, GFP_ATOMIC);
521 if (buf_copy == NULL)
523 msg_set_destnode(buf_msg(buf_copy), n_ptr->addr);
524 link_send(buf_copy, n_ptr->addr, n_ptr->addr);
531 * cluster_broadcast - broadcast message to all nodes within cluster
534 void cluster_broadcast(struct sk_buff *buf)
536 struct sk_buff *buf_copy;
537 struct cluster *c_ptr;
544 if (tipc_mode == TIPC_NET_MODE) {
545 c_ptr = cluster_find(tipc_own_addr);
546 assert(in_own_cluster(c_ptr->addr)); /* For now */
548 /* Send to standard nodes, then repeat loop sending to slaves */
550 tstop = c_ptr->highest_node;
551 for (node_type = 1; node_type <= 2; node_type++) {
552 for (n_num = tstart; n_num <= tstop; n_num++) {
553 n_ptr = c_ptr->nodes[n_num];
554 if (n_ptr && node_has_active_links(n_ptr)) {
555 buf_copy = skb_copy(buf, GFP_ATOMIC);
556 if (buf_copy == NULL)
558 msg_set_destnode(buf_msg(buf_copy),
560 link_send(buf_copy, n_ptr->addr,
564 tstart = LOWEST_SLAVE;
565 tstop = c_ptr->highest_slave;
572 int cluster_init(void)
574 highest_allowed_slave = LOWEST_SLAVE + tipc_max_slaves;
575 return cluster_create(tipc_own_addr) ? TIPC_OK : -ENOMEM;