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.
40 #include "node_subscr.h"
47 static void tipc_cltr_multicast(struct cluster
*c_ptr
, struct sk_buff
*buf
,
48 u32 lower
, u32 upper
);
49 static struct sk_buff
*tipc_cltr_prepare_routing_msg(u32 data_size
, u32 dest
);
51 struct tipc_node
**tipc_local_nodes
= NULL
;
52 struct tipc_node_map tipc_cltr_bcast_nodes
= {0,{0,}};
53 u32 tipc_highest_allowed_slave
= 0;
55 struct cluster
*tipc_cltr_create(u32 addr
)
58 struct cluster
*c_ptr
;
61 c_ptr
= kzalloc(sizeof(*c_ptr
), GFP_ATOMIC
);
63 warn("Cluster creation failure, no memory\n");
67 c_ptr
->addr
= tipc_addr(tipc_zone(addr
), tipc_cluster(addr
), 0);
68 if (in_own_cluster(addr
))
69 max_nodes
= LOWEST_SLAVE
+ tipc_max_slaves
;
71 max_nodes
= tipc_max_nodes
+ 1;
73 c_ptr
->nodes
= kcalloc(max_nodes
+ 1, sizeof(void*), GFP_ATOMIC
);
74 if (c_ptr
->nodes
== NULL
) {
75 warn("Cluster creation failure, no memory for node area\n");
80 if (in_own_cluster(addr
))
81 tipc_local_nodes
= c_ptr
->nodes
;
82 c_ptr
->highest_slave
= LOWEST_SLAVE
- 1;
83 c_ptr
->highest_node
= 0;
85 z_ptr
= tipc_zone_find(tipc_zone(addr
));
87 z_ptr
= tipc_zone_create(addr
);
95 tipc_zone_attach_cluster(z_ptr
, c_ptr
);
100 void tipc_cltr_delete(struct cluster
*c_ptr
)
106 for (n_num
= 1; n_num
<= c_ptr
->highest_node
; n_num
++) {
107 tipc_node_delete(c_ptr
->nodes
[n_num
]);
109 for (n_num
= LOWEST_SLAVE
; n_num
<= c_ptr
->highest_slave
; n_num
++) {
110 tipc_node_delete(c_ptr
->nodes
[n_num
]);
117 void tipc_cltr_attach_node(struct cluster
*c_ptr
, struct tipc_node
*n_ptr
)
119 u32 n_num
= tipc_node(n_ptr
->addr
);
120 u32 max_n_num
= tipc_max_nodes
;
122 if (in_own_cluster(n_ptr
->addr
))
123 max_n_num
= tipc_highest_allowed_slave
;
125 assert(n_num
<= max_n_num
);
126 assert(c_ptr
->nodes
[n_num
] == NULL
);
127 c_ptr
->nodes
[n_num
] = n_ptr
;
128 if (n_num
> c_ptr
->highest_node
)
129 c_ptr
->highest_node
= n_num
;
133 * tipc_cltr_select_router - select router to a cluster
135 * Uses deterministic and fair algorithm.
138 u32
tipc_cltr_select_router(struct cluster
*c_ptr
, u32 ref
)
141 u32 ulim
= c_ptr
->highest_node
;
145 assert(!in_own_cluster(c_ptr
->addr
));
149 /* Start entry must be random */
150 mask
= tipc_max_nodes
;
156 /* Lookup upwards with wrap-around */
158 if (tipc_node_is_up(c_ptr
->nodes
[n_num
]))
160 } while (++n_num
<= ulim
);
164 if (tipc_node_is_up(c_ptr
->nodes
[n_num
]))
166 } while (++n_num
< tstart
);
170 assert(n_num
<= ulim
);
171 return tipc_node_select_router(c_ptr
->nodes
[n_num
], ref
);
175 * tipc_cltr_select_node - select destination node within a remote cluster
177 * Uses deterministic and fair algorithm.
180 struct tipc_node
*tipc_cltr_select_node(struct cluster
*c_ptr
, u32 selector
)
183 u32 mask
= tipc_max_nodes
;
186 assert(!in_own_cluster(c_ptr
->addr
));
187 if (!c_ptr
->highest_node
)
190 /* Start entry must be random */
191 while (mask
> c_ptr
->highest_node
) {
194 start_entry
= (selector
& mask
) ? selector
& mask
: 1u;
195 assert(start_entry
<= c_ptr
->highest_node
);
197 /* Lookup upwards with wrap-around */
198 for (n_num
= start_entry
; n_num
<= c_ptr
->highest_node
; n_num
++) {
199 if (tipc_node_has_active_links(c_ptr
->nodes
[n_num
]))
200 return c_ptr
->nodes
[n_num
];
202 for (n_num
= 1; n_num
< start_entry
; n_num
++) {
203 if (tipc_node_has_active_links(c_ptr
->nodes
[n_num
]))
204 return c_ptr
->nodes
[n_num
];
210 * Routing table management: See description in node.c
213 static struct sk_buff
*tipc_cltr_prepare_routing_msg(u32 data_size
, u32 dest
)
215 u32 size
= INT_H_SIZE
+ data_size
;
216 struct sk_buff
*buf
= tipc_buf_acquire(size
);
217 struct tipc_msg
*msg
;
221 memset((char *)msg
, 0, size
);
222 tipc_msg_init(msg
, ROUTE_DISTRIBUTOR
, 0, INT_H_SIZE
, dest
);
227 void tipc_cltr_bcast_new_route(struct cluster
*c_ptr
, u32 dest
,
228 u32 lower
, u32 upper
)
230 struct sk_buff
*buf
= tipc_cltr_prepare_routing_msg(0, c_ptr
->addr
);
231 struct tipc_msg
*msg
;
235 msg_set_remote_node(msg
, dest
);
236 msg_set_type(msg
, ROUTE_ADDITION
);
237 tipc_cltr_multicast(c_ptr
, buf
, lower
, upper
);
239 warn("Memory squeeze: broadcast of new route failed\n");
243 void tipc_cltr_bcast_lost_route(struct cluster
*c_ptr
, u32 dest
,
244 u32 lower
, u32 upper
)
246 struct sk_buff
*buf
= tipc_cltr_prepare_routing_msg(0, c_ptr
->addr
);
247 struct tipc_msg
*msg
;
251 msg_set_remote_node(msg
, dest
);
252 msg_set_type(msg
, ROUTE_REMOVAL
);
253 tipc_cltr_multicast(c_ptr
, buf
, lower
, upper
);
255 warn("Memory squeeze: broadcast of lost route failed\n");
259 void tipc_cltr_send_slave_routes(struct cluster
*c_ptr
, u32 dest
)
262 struct tipc_msg
*msg
;
263 u32 highest
= c_ptr
->highest_slave
;
267 assert(!is_slave(dest
));
268 assert(in_own_cluster(dest
));
269 assert(in_own_cluster(c_ptr
->addr
));
270 if (highest
<= LOWEST_SLAVE
)
272 buf
= tipc_cltr_prepare_routing_msg(highest
- LOWEST_SLAVE
+ 1,
276 msg_set_remote_node(msg
, c_ptr
->addr
);
277 msg_set_type(msg
, SLAVE_ROUTING_TABLE
);
278 for (n_num
= LOWEST_SLAVE
; n_num
<= highest
; n_num
++) {
279 if (c_ptr
->nodes
[n_num
] &&
280 tipc_node_has_active_links(c_ptr
->nodes
[n_num
])) {
282 msg_set_dataoctet(msg
, n_num
);
286 tipc_link_send(buf
, dest
, dest
);
290 warn("Memory squeeze: broadcast of lost route failed\n");
294 void tipc_cltr_send_ext_routes(struct cluster
*c_ptr
, u32 dest
)
297 struct tipc_msg
*msg
;
298 u32 highest
= c_ptr
->highest_node
;
302 if (in_own_cluster(c_ptr
->addr
))
304 assert(!is_slave(dest
));
305 assert(in_own_cluster(dest
));
306 highest
= c_ptr
->highest_node
;
307 buf
= tipc_cltr_prepare_routing_msg(highest
+ 1, c_ptr
->addr
);
310 msg_set_remote_node(msg
, c_ptr
->addr
);
311 msg_set_type(msg
, EXT_ROUTING_TABLE
);
312 for (n_num
= 1; n_num
<= highest
; n_num
++) {
313 if (c_ptr
->nodes
[n_num
] &&
314 tipc_node_has_active_links(c_ptr
->nodes
[n_num
])) {
316 msg_set_dataoctet(msg
, n_num
);
320 tipc_link_send(buf
, dest
, dest
);
324 warn("Memory squeeze: broadcast of external route failed\n");
328 void tipc_cltr_send_local_routes(struct cluster
*c_ptr
, u32 dest
)
331 struct tipc_msg
*msg
;
332 u32 highest
= c_ptr
->highest_node
;
336 assert(is_slave(dest
));
337 assert(in_own_cluster(c_ptr
->addr
));
338 buf
= tipc_cltr_prepare_routing_msg(highest
, c_ptr
->addr
);
341 msg_set_remote_node(msg
, c_ptr
->addr
);
342 msg_set_type(msg
, LOCAL_ROUTING_TABLE
);
343 for (n_num
= 1; n_num
<= highest
; n_num
++) {
344 if (c_ptr
->nodes
[n_num
] &&
345 tipc_node_has_active_links(c_ptr
->nodes
[n_num
])) {
347 msg_set_dataoctet(msg
, n_num
);
351 tipc_link_send(buf
, dest
, dest
);
355 warn("Memory squeeze: broadcast of local route failed\n");
359 void tipc_cltr_recv_routing_table(struct sk_buff
*buf
)
361 struct tipc_msg
*msg
= buf_msg(buf
);
362 struct cluster
*c_ptr
;
363 struct tipc_node
*n_ptr
;
367 u32 rem_node
= msg_remote_node(msg
);
372 c_ptr
= tipc_cltr_find(rem_node
);
374 c_ptr
= tipc_cltr_create(rem_node
);
381 node_table
= buf
->data
+ msg_hdr_sz(msg
);
382 table_size
= msg_size(msg
) - msg_hdr_sz(msg
);
383 router
= msg_prevnode(msg
);
384 z_num
= tipc_zone(rem_node
);
385 c_num
= tipc_cluster(rem_node
);
387 switch (msg_type(msg
)) {
388 case LOCAL_ROUTING_TABLE
:
389 assert(is_slave(tipc_own_addr
));
390 case EXT_ROUTING_TABLE
:
391 for (n_num
= 1; n_num
< table_size
; n_num
++) {
392 if (node_table
[n_num
]) {
393 u32 addr
= tipc_addr(z_num
, c_num
, n_num
);
394 n_ptr
= c_ptr
->nodes
[n_num
];
396 n_ptr
= tipc_node_create(addr
);
399 tipc_node_add_router(n_ptr
, router
);
403 case SLAVE_ROUTING_TABLE
:
404 assert(!is_slave(tipc_own_addr
));
405 assert(in_own_cluster(c_ptr
->addr
));
406 for (n_num
= 1; n_num
< table_size
; n_num
++) {
407 if (node_table
[n_num
]) {
408 u32 slave_num
= n_num
+ LOWEST_SLAVE
;
409 u32 addr
= tipc_addr(z_num
, c_num
, slave_num
);
410 n_ptr
= c_ptr
->nodes
[slave_num
];
412 n_ptr
= tipc_node_create(addr
);
415 tipc_node_add_router(n_ptr
, router
);
420 if (!is_slave(tipc_own_addr
)) {
421 assert(!in_own_cluster(c_ptr
->addr
) ||
424 assert(in_own_cluster(c_ptr
->addr
) &&
425 !is_slave(rem_node
));
427 n_ptr
= c_ptr
->nodes
[tipc_node(rem_node
)];
429 n_ptr
= tipc_node_create(rem_node
);
431 tipc_node_add_router(n_ptr
, router
);
434 if (!is_slave(tipc_own_addr
)) {
435 assert(!in_own_cluster(c_ptr
->addr
) ||
438 assert(in_own_cluster(c_ptr
->addr
) &&
439 !is_slave(rem_node
));
441 n_ptr
= c_ptr
->nodes
[tipc_node(rem_node
)];
443 tipc_node_remove_router(n_ptr
, router
);
446 assert(!"Illegal routing manager message received\n");
451 void tipc_cltr_remove_as_router(struct cluster
*c_ptr
, u32 router
)
457 if (is_slave(router
))
458 return; /* Slave nodes can not be routers */
460 if (in_own_cluster(c_ptr
->addr
)) {
461 start_entry
= LOWEST_SLAVE
;
462 tstop
= c_ptr
->highest_slave
;
465 tstop
= c_ptr
->highest_node
;
468 for (n_num
= start_entry
; n_num
<= tstop
; n_num
++) {
469 if (c_ptr
->nodes
[n_num
]) {
470 tipc_node_remove_router(c_ptr
->nodes
[n_num
], router
);
476 * tipc_cltr_multicast - multicast message to local nodes
479 static void tipc_cltr_multicast(struct cluster
*c_ptr
, struct sk_buff
*buf
,
480 u32 lower
, u32 upper
)
482 struct sk_buff
*buf_copy
;
483 struct tipc_node
*n_ptr
;
487 assert(lower
<= upper
);
488 assert(((lower
>= 1) && (lower
<= tipc_max_nodes
)) ||
489 ((lower
>= LOWEST_SLAVE
) && (lower
<= tipc_highest_allowed_slave
)));
490 assert(((upper
>= 1) && (upper
<= tipc_max_nodes
)) ||
491 ((upper
>= LOWEST_SLAVE
) && (upper
<= tipc_highest_allowed_slave
)));
492 assert(in_own_cluster(c_ptr
->addr
));
494 tstop
= is_slave(upper
) ? c_ptr
->highest_slave
: c_ptr
->highest_node
;
497 for (n_num
= lower
; n_num
<= tstop
; n_num
++) {
498 n_ptr
= c_ptr
->nodes
[n_num
];
499 if (n_ptr
&& tipc_node_has_active_links(n_ptr
)) {
500 buf_copy
= skb_copy(buf
, GFP_ATOMIC
);
501 if (buf_copy
== NULL
)
503 msg_set_destnode(buf_msg(buf_copy
), n_ptr
->addr
);
504 tipc_link_send(buf_copy
, n_ptr
->addr
, n_ptr
->addr
);
511 * tipc_cltr_broadcast - broadcast message to all nodes within cluster
514 void tipc_cltr_broadcast(struct sk_buff
*buf
)
516 struct sk_buff
*buf_copy
;
517 struct cluster
*c_ptr
;
518 struct tipc_node
*n_ptr
;
524 if (tipc_mode
== TIPC_NET_MODE
) {
525 c_ptr
= tipc_cltr_find(tipc_own_addr
);
526 assert(in_own_cluster(c_ptr
->addr
)); /* For now */
528 /* Send to standard nodes, then repeat loop sending to slaves */
530 tstop
= c_ptr
->highest_node
;
531 for (node_type
= 1; node_type
<= 2; node_type
++) {
532 for (n_num
= tstart
; n_num
<= tstop
; n_num
++) {
533 n_ptr
= c_ptr
->nodes
[n_num
];
534 if (n_ptr
&& tipc_node_has_active_links(n_ptr
)) {
535 buf_copy
= skb_copy(buf
, GFP_ATOMIC
);
536 if (buf_copy
== NULL
)
538 msg_set_destnode(buf_msg(buf_copy
),
540 tipc_link_send(buf_copy
, n_ptr
->addr
,
544 tstart
= LOWEST_SLAVE
;
545 tstop
= c_ptr
->highest_slave
;
552 int tipc_cltr_init(void)
554 tipc_highest_allowed_slave
= LOWEST_SLAVE
+ tipc_max_slaves
;
555 return tipc_cltr_create(tipc_own_addr
) ? 0 : -ENOMEM
;