[TIPC]: Enhanced & cleaned up system messages; fixed 2 obscure memory leaks.
[safe/jmp/linux-2.6] / net / tipc / cluster.c
1 /*
2  * net/tipc/cluster.c: TIPC cluster management routines
3  * 
4  * Copyright (c) 2000-2006, Ericsson AB
5  * Copyright (c) 2005, Wind River Systems
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
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.
19  *
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.
23  *
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.
35  */
36
37 #include "core.h"
38 #include "cluster.h"
39 #include "addr.h"
40 #include "node_subscr.h"
41 #include "link.h"
42 #include "node.h"
43 #include "net.h"
44 #include "msg.h"
45 #include "bearer.h"
46
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);
50
51 struct node **tipc_local_nodes = NULL;
52 struct node_map tipc_cltr_bcast_nodes = {0,{0,}};
53 u32 tipc_highest_allowed_slave = 0;
54
55 struct cluster *tipc_cltr_create(u32 addr)
56 {
57         struct _zone *z_ptr;
58         struct cluster *c_ptr;
59         int max_nodes; 
60         int alloc;
61
62         c_ptr = (struct cluster *)kmalloc(sizeof(*c_ptr), GFP_ATOMIC);
63         if (c_ptr == NULL) {
64                 warn("Cluster creation failure, no memory\n");
65                 return NULL;
66         }
67         memset(c_ptr, 0, sizeof(*c_ptr));
68
69         c_ptr->addr = tipc_addr(tipc_zone(addr), tipc_cluster(addr), 0);
70         if (in_own_cluster(addr))
71                 max_nodes = LOWEST_SLAVE + tipc_max_slaves;
72         else
73                 max_nodes = tipc_max_nodes + 1;
74         alloc = sizeof(void *) * (max_nodes + 1);
75
76         c_ptr->nodes = (struct node **)kmalloc(alloc, GFP_ATOMIC);
77         if (c_ptr->nodes == NULL) {
78                 warn("Cluster creation failure, no memory for node area\n");
79                 kfree(c_ptr);
80                 return NULL;
81         }
82         memset(c_ptr->nodes, 0, alloc);
83
84         if (in_own_cluster(addr))
85                 tipc_local_nodes = c_ptr->nodes;
86         c_ptr->highest_slave = LOWEST_SLAVE - 1;
87         c_ptr->highest_node = 0;
88         
89         z_ptr = tipc_zone_find(tipc_zone(addr));
90         if (!z_ptr) {
91                 z_ptr = tipc_zone_create(addr);
92         }
93         if (!z_ptr) {
94                 kfree(c_ptr->nodes);
95                 kfree(c_ptr);
96                 return NULL;
97         }
98
99         tipc_zone_attach_cluster(z_ptr, c_ptr);
100         c_ptr->owner = z_ptr;
101         return c_ptr;
102 }
103
104 void tipc_cltr_delete(struct cluster *c_ptr)
105 {
106         u32 n_num;
107
108         if (!c_ptr)
109                 return;
110         for (n_num = 1; n_num <= c_ptr->highest_node; n_num++) {
111                 tipc_node_delete(c_ptr->nodes[n_num]);
112         }
113         for (n_num = LOWEST_SLAVE; n_num <= c_ptr->highest_slave; n_num++) {
114                 tipc_node_delete(c_ptr->nodes[n_num]);
115         }
116         kfree(c_ptr->nodes);
117         kfree(c_ptr);
118 }
119
120 u32 tipc_cltr_next_node(struct cluster *c_ptr, u32 addr)
121 {
122         struct node *n_ptr;
123         u32 n_num = tipc_node(addr) + 1;
124
125         if (!c_ptr)
126                 return addr;
127         for (; n_num <= c_ptr->highest_node; n_num++) {
128                 n_ptr = c_ptr->nodes[n_num];
129                 if (n_ptr && tipc_node_has_active_links(n_ptr))
130                         return n_ptr->addr;
131         }
132         for (n_num = 1; n_num < tipc_node(addr); n_num++) {
133                 n_ptr = c_ptr->nodes[n_num];
134                 if (n_ptr && tipc_node_has_active_links(n_ptr))
135                         return n_ptr->addr;
136         }
137         return 0;
138 }
139
140 void tipc_cltr_attach_node(struct cluster *c_ptr, struct node *n_ptr)
141 {
142         u32 n_num = tipc_node(n_ptr->addr);
143         u32 max_n_num = tipc_max_nodes;
144
145         if (in_own_cluster(n_ptr->addr))
146                 max_n_num = tipc_highest_allowed_slave;
147         assert(n_num > 0);
148         assert(n_num <= max_n_num);
149         assert(c_ptr->nodes[n_num] == 0);
150         c_ptr->nodes[n_num] = n_ptr;
151         if (n_num > c_ptr->highest_node)
152                 c_ptr->highest_node = n_num;
153 }
154
155 /**
156  * tipc_cltr_select_router - select router to a cluster
157  * 
158  * Uses deterministic and fair algorithm.
159  */
160
161 u32 tipc_cltr_select_router(struct cluster *c_ptr, u32 ref)
162 {
163         u32 n_num;
164         u32 ulim = c_ptr->highest_node;
165         u32 mask;
166         u32 tstart;
167
168         assert(!in_own_cluster(c_ptr->addr));
169         if (!ulim)
170                 return 0;
171
172         /* Start entry must be random */
173         mask = tipc_max_nodes;
174         while (mask > ulim)
175                 mask >>= 1;
176         tstart = ref & mask;
177         n_num = tstart;
178
179         /* Lookup upwards with wrap-around */
180         do {
181                 if (tipc_node_is_up(c_ptr->nodes[n_num]))
182                         break;
183         } while (++n_num <= ulim);
184         if (n_num > ulim) {
185                 n_num = 1;
186                 do {
187                         if (tipc_node_is_up(c_ptr->nodes[n_num]))
188                                 break;
189                 } while (++n_num < tstart);
190                 if (n_num == tstart)
191                         return 0;
192         }
193         assert(n_num <= ulim);
194         return tipc_node_select_router(c_ptr->nodes[n_num], ref);
195 }
196
197 /**
198  * tipc_cltr_select_node - select destination node within a remote cluster
199  * 
200  * Uses deterministic and fair algorithm.
201  */
202
203 struct node *tipc_cltr_select_node(struct cluster *c_ptr, u32 selector)
204 {
205         u32 n_num;
206         u32 mask = tipc_max_nodes;
207         u32 start_entry;
208
209         assert(!in_own_cluster(c_ptr->addr));
210         if (!c_ptr->highest_node)
211                 return NULL;
212
213         /* Start entry must be random */
214         while (mask > c_ptr->highest_node) {
215                 mask >>= 1;
216         }
217         start_entry = (selector & mask) ? selector & mask : 1u;
218         assert(start_entry <= c_ptr->highest_node);
219
220         /* Lookup upwards with wrap-around */
221         for (n_num = start_entry; n_num <= c_ptr->highest_node; n_num++) {
222                 if (tipc_node_has_active_links(c_ptr->nodes[n_num]))
223                         return c_ptr->nodes[n_num];
224         }
225         for (n_num = 1; n_num < start_entry; n_num++) {
226                 if (tipc_node_has_active_links(c_ptr->nodes[n_num]))
227                         return c_ptr->nodes[n_num];
228         }
229         return NULL;
230 }
231
232 /*
233  *    Routing table management: See description in node.c
234  */
235
236 static struct sk_buff *tipc_cltr_prepare_routing_msg(u32 data_size, u32 dest)
237 {
238         u32 size = INT_H_SIZE + data_size;
239         struct sk_buff *buf = buf_acquire(size);
240         struct tipc_msg *msg;
241
242         if (buf) {
243                 msg = buf_msg(buf);
244                 memset((char *)msg, 0, size);
245                 msg_init(msg, ROUTE_DISTRIBUTOR, 0, TIPC_OK, INT_H_SIZE, dest);
246         }
247         return buf;
248 }
249
250 void tipc_cltr_bcast_new_route(struct cluster *c_ptr, u32 dest,
251                              u32 lower, u32 upper)
252 {
253         struct sk_buff *buf = tipc_cltr_prepare_routing_msg(0, c_ptr->addr);
254         struct tipc_msg *msg;
255
256         if (buf) {
257                 msg = buf_msg(buf);
258                 msg_set_remote_node(msg, dest);
259                 msg_set_type(msg, ROUTE_ADDITION);
260                 tipc_cltr_multicast(c_ptr, buf, lower, upper);
261         } else {
262                 warn("Memory squeeze: broadcast of new route failed\n");
263         }
264 }
265
266 void tipc_cltr_bcast_lost_route(struct cluster *c_ptr, u32 dest,
267                                 u32 lower, u32 upper)
268 {
269         struct sk_buff *buf = tipc_cltr_prepare_routing_msg(0, c_ptr->addr);
270         struct tipc_msg *msg;
271
272         if (buf) {
273                 msg = buf_msg(buf);
274                 msg_set_remote_node(msg, dest);
275                 msg_set_type(msg, ROUTE_REMOVAL);
276                 tipc_cltr_multicast(c_ptr, buf, lower, upper);
277         } else {
278                 warn("Memory squeeze: broadcast of lost route failed\n");
279         }
280 }
281
282 void tipc_cltr_send_slave_routes(struct cluster *c_ptr, u32 dest)
283 {
284         struct sk_buff *buf;
285         struct tipc_msg *msg;
286         u32 highest = c_ptr->highest_slave;
287         u32 n_num;
288         int send = 0;
289
290         assert(!is_slave(dest));
291         assert(in_own_cluster(dest));
292         assert(in_own_cluster(c_ptr->addr));
293         if (highest <= LOWEST_SLAVE)
294                 return;
295         buf = tipc_cltr_prepare_routing_msg(highest - LOWEST_SLAVE + 1,
296                                             c_ptr->addr);
297         if (buf) {
298                 msg = buf_msg(buf);
299                 msg_set_remote_node(msg, c_ptr->addr);
300                 msg_set_type(msg, SLAVE_ROUTING_TABLE);
301                 for (n_num = LOWEST_SLAVE; n_num <= highest; n_num++) {
302                         if (c_ptr->nodes[n_num] && 
303                             tipc_node_has_active_links(c_ptr->nodes[n_num])) {
304                                 send = 1;
305                                 msg_set_dataoctet(msg, n_num);
306                         }
307                 }
308                 if (send)
309                         tipc_link_send(buf, dest, dest);
310                 else
311                         buf_discard(buf);
312         } else {
313                 warn("Memory squeeze: broadcast of lost route failed\n");
314         }
315 }
316
317 void tipc_cltr_send_ext_routes(struct cluster *c_ptr, u32 dest)
318 {
319         struct sk_buff *buf;
320         struct tipc_msg *msg;
321         u32 highest = c_ptr->highest_node;
322         u32 n_num;
323         int send = 0;
324
325         if (in_own_cluster(c_ptr->addr))
326                 return;
327         assert(!is_slave(dest));
328         assert(in_own_cluster(dest));
329         highest = c_ptr->highest_node;
330         buf = tipc_cltr_prepare_routing_msg(highest + 1, c_ptr->addr);
331         if (buf) {
332                 msg = buf_msg(buf);
333                 msg_set_remote_node(msg, c_ptr->addr);
334                 msg_set_type(msg, EXT_ROUTING_TABLE);
335                 for (n_num = 1; n_num <= highest; n_num++) {
336                         if (c_ptr->nodes[n_num] && 
337                             tipc_node_has_active_links(c_ptr->nodes[n_num])) {
338                                 send = 1;
339                                 msg_set_dataoctet(msg, n_num);
340                         }
341                 }
342                 if (send)
343                         tipc_link_send(buf, dest, dest);
344                 else
345                         buf_discard(buf);
346         } else {
347                 warn("Memory squeeze: broadcast of external route failed\n");
348         }
349 }
350
351 void tipc_cltr_send_local_routes(struct cluster *c_ptr, u32 dest)
352 {
353         struct sk_buff *buf;
354         struct tipc_msg *msg;
355         u32 highest = c_ptr->highest_node;
356         u32 n_num;
357         int send = 0;
358
359         assert(is_slave(dest));
360         assert(in_own_cluster(c_ptr->addr));
361         buf = tipc_cltr_prepare_routing_msg(highest, c_ptr->addr);
362         if (buf) {
363                 msg = buf_msg(buf);
364                 msg_set_remote_node(msg, c_ptr->addr);
365                 msg_set_type(msg, LOCAL_ROUTING_TABLE);
366                 for (n_num = 1; n_num <= highest; n_num++) {
367                         if (c_ptr->nodes[n_num] && 
368                             tipc_node_has_active_links(c_ptr->nodes[n_num])) {
369                                 send = 1;
370                                 msg_set_dataoctet(msg, n_num);
371                         }
372                 }
373                 if (send)
374                         tipc_link_send(buf, dest, dest);
375                 else
376                         buf_discard(buf);
377         } else {
378                 warn("Memory squeeze: broadcast of local route failed\n");
379         }
380 }
381
382 void tipc_cltr_recv_routing_table(struct sk_buff *buf)
383 {
384         struct tipc_msg *msg = buf_msg(buf);
385         struct cluster *c_ptr;
386         struct node *n_ptr;
387         unchar *node_table;
388         u32 table_size;
389         u32 router;
390         u32 rem_node = msg_remote_node(msg);
391         u32 z_num;
392         u32 c_num;
393         u32 n_num;
394
395         c_ptr = tipc_cltr_find(rem_node);
396         if (!c_ptr) {
397                 c_ptr = tipc_cltr_create(rem_node);
398                 if (!c_ptr) {
399                         buf_discard(buf);
400                         return;
401                 }
402         }
403
404         node_table = buf->data + msg_hdr_sz(msg);
405         table_size = msg_size(msg) - msg_hdr_sz(msg);
406         router = msg_prevnode(msg);
407         z_num = tipc_zone(rem_node);
408         c_num = tipc_cluster(rem_node);
409
410         switch (msg_type(msg)) {
411         case LOCAL_ROUTING_TABLE:
412                 assert(is_slave(tipc_own_addr));
413         case EXT_ROUTING_TABLE:
414                 for (n_num = 1; n_num < table_size; n_num++) {
415                         if (node_table[n_num]) {
416                                 u32 addr = tipc_addr(z_num, c_num, n_num);
417                                 n_ptr = c_ptr->nodes[n_num];
418                                 if (!n_ptr) {
419                                         n_ptr = tipc_node_create(addr);
420                                 }
421                                 if (n_ptr)
422                                         tipc_node_add_router(n_ptr, router);
423                         }
424                 }
425                 break;
426         case SLAVE_ROUTING_TABLE:
427                 assert(!is_slave(tipc_own_addr));
428                 assert(in_own_cluster(c_ptr->addr));
429                 for (n_num = 1; n_num < table_size; n_num++) {
430                         if (node_table[n_num]) {
431                                 u32 slave_num = n_num + LOWEST_SLAVE;
432                                 u32 addr = tipc_addr(z_num, c_num, slave_num);
433                                 n_ptr = c_ptr->nodes[slave_num];
434                                 if (!n_ptr) {
435                                         n_ptr = tipc_node_create(addr);
436                                 }
437                                 if (n_ptr)
438                                         tipc_node_add_router(n_ptr, router);
439                         }
440                 }
441                 break;
442         case ROUTE_ADDITION:
443                 if (!is_slave(tipc_own_addr)) {
444                         assert(!in_own_cluster(c_ptr->addr)
445                                || is_slave(rem_node));
446                 } else {
447                         assert(in_own_cluster(c_ptr->addr)
448                                && !is_slave(rem_node));
449                 }
450                 n_ptr = c_ptr->nodes[tipc_node(rem_node)];
451                 if (!n_ptr)
452                         n_ptr = tipc_node_create(rem_node);
453                 if (n_ptr)
454                         tipc_node_add_router(n_ptr, router);
455                 break;
456         case ROUTE_REMOVAL:
457                 if (!is_slave(tipc_own_addr)) {
458                         assert(!in_own_cluster(c_ptr->addr)
459                                || is_slave(rem_node));
460                 } else {
461                         assert(in_own_cluster(c_ptr->addr)
462                                && !is_slave(rem_node));
463                 }
464                 n_ptr = c_ptr->nodes[tipc_node(rem_node)];
465                 if (n_ptr)
466                         tipc_node_remove_router(n_ptr, router);
467                 break;
468         default:
469                 assert(!"Illegal routing manager message received\n");
470         }
471         buf_discard(buf);
472 }
473
474 void tipc_cltr_remove_as_router(struct cluster *c_ptr, u32 router)
475 {
476         u32 start_entry;
477         u32 tstop;
478         u32 n_num;
479
480         if (is_slave(router))
481                 return; /* Slave nodes can not be routers */
482
483         if (in_own_cluster(c_ptr->addr)) {
484                 start_entry = LOWEST_SLAVE;
485                 tstop = c_ptr->highest_slave;
486         } else {
487                 start_entry = 1;
488                 tstop = c_ptr->highest_node;
489         }
490
491         for (n_num = start_entry; n_num <= tstop; n_num++) {
492                 if (c_ptr->nodes[n_num]) {
493                         tipc_node_remove_router(c_ptr->nodes[n_num], router);
494                 }
495         }
496 }
497
498 /**
499  * tipc_cltr_multicast - multicast message to local nodes 
500  */
501
502 static void tipc_cltr_multicast(struct cluster *c_ptr, struct sk_buff *buf,
503                          u32 lower, u32 upper)
504 {
505         struct sk_buff *buf_copy;
506         struct node *n_ptr;
507         u32 n_num;
508         u32 tstop;
509
510         assert(lower <= upper);
511         assert(((lower >= 1) && (lower <= tipc_max_nodes)) ||
512                ((lower >= LOWEST_SLAVE) && (lower <= tipc_highest_allowed_slave)));
513         assert(((upper >= 1) && (upper <= tipc_max_nodes)) ||
514                ((upper >= LOWEST_SLAVE) && (upper <= tipc_highest_allowed_slave)));
515         assert(in_own_cluster(c_ptr->addr));
516
517         tstop = is_slave(upper) ? c_ptr->highest_slave : c_ptr->highest_node;
518         if (tstop > upper)
519                 tstop = upper;
520         for (n_num = lower; n_num <= tstop; n_num++) {
521                 n_ptr = c_ptr->nodes[n_num];
522                 if (n_ptr && tipc_node_has_active_links(n_ptr)) {
523                         buf_copy = skb_copy(buf, GFP_ATOMIC);
524                         if (buf_copy == NULL)
525                                 break;
526                         msg_set_destnode(buf_msg(buf_copy), n_ptr->addr);
527                         tipc_link_send(buf_copy, n_ptr->addr, n_ptr->addr);
528                 }
529         }
530         buf_discard(buf);
531 }
532
533 /**
534  * tipc_cltr_broadcast - broadcast message to all nodes within cluster
535  */
536
537 void tipc_cltr_broadcast(struct sk_buff *buf)
538 {
539         struct sk_buff *buf_copy;
540         struct cluster *c_ptr;
541         struct node *n_ptr;
542         u32 n_num;
543         u32 tstart;
544         u32 tstop;
545         u32 node_type;
546
547         if (tipc_mode == TIPC_NET_MODE) {
548                 c_ptr = tipc_cltr_find(tipc_own_addr);
549                 assert(in_own_cluster(c_ptr->addr));    /* For now */
550
551                 /* Send to standard nodes, then repeat loop sending to slaves */
552                 tstart = 1;
553                 tstop = c_ptr->highest_node;
554                 for (node_type = 1; node_type <= 2; node_type++) {
555                         for (n_num = tstart; n_num <= tstop; n_num++) {
556                                 n_ptr = c_ptr->nodes[n_num];
557                                 if (n_ptr && tipc_node_has_active_links(n_ptr)) {
558                                         buf_copy = skb_copy(buf, GFP_ATOMIC);
559                                         if (buf_copy == NULL)
560                                                 goto exit;
561                                         msg_set_destnode(buf_msg(buf_copy), 
562                                                          n_ptr->addr);
563                                         tipc_link_send(buf_copy, n_ptr->addr, 
564                                                        n_ptr->addr);
565                                 }
566                         }
567                         tstart = LOWEST_SLAVE;
568                         tstop = c_ptr->highest_slave;
569                 }
570         }
571 exit:
572         buf_discard(buf);
573 }
574
575 int tipc_cltr_init(void)
576 {
577         tipc_highest_allowed_slave = LOWEST_SLAVE + tipc_max_slaves;
578         return tipc_cltr_create(tipc_own_addr) ? TIPC_OK : -ENOMEM;
579 }
580