]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/tipc/node.c
Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[net-next-2.6.git] / net / tipc / node.c
CommitLineData
b97bf3fd
PL
1/*
2 * net/tipc/node.c: TIPC node management routines
c4307285 3 *
593a5f22 4 * Copyright (c) 2000-2006, Ericsson AB
ea13847b 5 * Copyright (c) 2005-2006, Wind River Systems
b97bf3fd
PL
6 * All rights reserved.
7 *
9ea1fd3c 8 * Redistribution and use in source and binary forms, with or without
b97bf3fd
PL
9 * modification, are permitted provided that the following conditions are met:
10 *
9ea1fd3c
PL
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.
b97bf3fd 19 *
9ea1fd3c
PL
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
b97bf3fd
PL
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37#include "core.h"
38#include "config.h"
39#include "node.h"
40#include "cluster.h"
41#include "net.h"
42#include "addr.h"
43#include "node_subscr.h"
44#include "link.h"
45#include "port.h"
46#include "bearer.h"
47#include "name_distr.h"
b97bf3fd 48
6c00055a
DM
49void node_print(struct print_buf *buf, struct tipc_node *n_ptr, char *str);
50static void node_lost_contact(struct tipc_node *n_ptr);
51static void node_established_contact(struct tipc_node *n_ptr);
b97bf3fd 52
6c00055a 53struct tipc_node *tipc_nodes = NULL; /* sorted list of nodes within cluster */
b97bf3fd 54
2ecb0924
AS
55static DEFINE_SPINLOCK(node_create_lock);
56
b97bf3fd
PL
57u32 tipc_own_tag = 0;
58
2ecb0924
AS
59/**
60 * tipc_node_create - create neighboring node
61 *
62 * Currently, this routine is called by neighbor discovery code, which holds
63 * net_lock for reading only. We must take node_create_lock to ensure a node
64 * isn't created twice if two different bearers discover the node at the same
65 * time. (It would be preferable to switch to holding net_lock in write mode,
66 * but this is a non-trivial change.)
67 */
68
6c00055a 69struct tipc_node *tipc_node_create(u32 addr)
b97bf3fd
PL
70{
71 struct cluster *c_ptr;
6c00055a
DM
72 struct tipc_node *n_ptr;
73 struct tipc_node **curr_node;
b97bf3fd 74
2ecb0924
AS
75 spin_lock_bh(&node_create_lock);
76
77 for (n_ptr = tipc_nodes; n_ptr; n_ptr = n_ptr->next) {
78 if (addr < n_ptr->addr)
79 break;
80 if (addr == n_ptr->addr) {
81 spin_unlock_bh(&node_create_lock);
82 return n_ptr;
83 }
84 }
85
2710b57f 86 n_ptr = kzalloc(sizeof(*n_ptr),GFP_ATOMIC);
a10bd924 87 if (!n_ptr) {
2ecb0924 88 spin_unlock_bh(&node_create_lock);
a10bd924
AS
89 warn("Node creation failed, no memory\n");
90 return NULL;
91 }
92
93 c_ptr = tipc_cltr_find(addr);
94 if (!c_ptr) {
95 c_ptr = tipc_cltr_create(addr);
96 }
97 if (!c_ptr) {
2ecb0924 98 spin_unlock_bh(&node_create_lock);
a10bd924
AS
99 kfree(n_ptr);
100 return NULL;
101 }
c4307285 102
a10bd924 103 n_ptr->addr = addr;
c4307285 104 spin_lock_init(&n_ptr->lock);
a10bd924
AS
105 INIT_LIST_HEAD(&n_ptr->nsub);
106 n_ptr->owner = c_ptr;
107 tipc_cltr_attach_node(c_ptr, n_ptr);
108 n_ptr->last_router = -1;
109
110 /* Insert node into ordered list */
c4307285 111 for (curr_node = &tipc_nodes; *curr_node;
a10bd924
AS
112 curr_node = &(*curr_node)->next) {
113 if (addr < (*curr_node)->addr) {
114 n_ptr->next = *curr_node;
115 break;
116 }
117 }
118 (*curr_node) = n_ptr;
2ecb0924 119 spin_unlock_bh(&node_create_lock);
b97bf3fd
PL
120 return n_ptr;
121}
122
6c00055a 123void tipc_node_delete(struct tipc_node *n_ptr)
b97bf3fd
PL
124{
125 if (!n_ptr)
126 return;
127
128#if 0
4323add6 129 /* Not needed because links are already deleted via tipc_bearer_stop() */
b97bf3fd
PL
130
131 u32 l_num;
132
133 for (l_num = 0; l_num < MAX_BEARERS; l_num++) {
134 link_delete(n_ptr->links[l_num]);
135 }
136#endif
137
138 dbg("node %x deleted\n", n_ptr->addr);
139 kfree(n_ptr);
140}
141
142
143/**
4323add6 144 * tipc_node_link_up - handle addition of link
c4307285 145 *
b97bf3fd
PL
146 * Link becomes active (alone or shared) or standby, depending on its priority.
147 */
148
6c00055a 149void tipc_node_link_up(struct tipc_node *n_ptr, struct link *l_ptr)
b97bf3fd
PL
150{
151 struct link **active = &n_ptr->active_links[0];
152
5392d646
AS
153 n_ptr->working_links++;
154
b97bf3fd
PL
155 info("Established link <%s> on network plane %c\n",
156 l_ptr->name, l_ptr->b_ptr->net_plane);
c4307285 157
b97bf3fd
PL
158 if (!active[0]) {
159 dbg(" link %x into %x/%x\n", l_ptr, &active[0], &active[1]);
160 active[0] = active[1] = l_ptr;
161 node_established_contact(n_ptr);
162 return;
163 }
c4307285 164 if (l_ptr->priority < active[0]->priority) {
a10bd924 165 info("New link <%s> becomes standby\n", l_ptr->name);
b97bf3fd
PL
166 return;
167 }
4323add6 168 tipc_link_send_duplicate(active[0], l_ptr);
c4307285 169 if (l_ptr->priority == active[0]->priority) {
b97bf3fd
PL
170 active[0] = l_ptr;
171 return;
172 }
a10bd924
AS
173 info("Old link <%s> becomes standby\n", active[0]->name);
174 if (active[1] != active[0])
175 info("Old link <%s> becomes standby\n", active[1]->name);
b97bf3fd
PL
176 active[0] = active[1] = l_ptr;
177}
178
179/**
180 * node_select_active_links - select active link
181 */
182
6c00055a 183static void node_select_active_links(struct tipc_node *n_ptr)
b97bf3fd
PL
184{
185 struct link **active = &n_ptr->active_links[0];
186 u32 i;
187 u32 highest_prio = 0;
188
c4307285 189 active[0] = active[1] = NULL;
b97bf3fd
PL
190
191 for (i = 0; i < MAX_BEARERS; i++) {
c4307285 192 struct link *l_ptr = n_ptr->links[i];
b97bf3fd 193
4323add6 194 if (!l_ptr || !tipc_link_is_up(l_ptr) ||
b97bf3fd
PL
195 (l_ptr->priority < highest_prio))
196 continue;
197
198 if (l_ptr->priority > highest_prio) {
c4307285 199 highest_prio = l_ptr->priority;
b97bf3fd
PL
200 active[0] = active[1] = l_ptr;
201 } else {
202 active[1] = l_ptr;
203 }
204 }
205}
206
207/**
4323add6 208 * tipc_node_link_down - handle loss of link
b97bf3fd
PL
209 */
210
6c00055a 211void tipc_node_link_down(struct tipc_node *n_ptr, struct link *l_ptr)
b97bf3fd
PL
212{
213 struct link **active;
214
5392d646
AS
215 n_ptr->working_links--;
216
4323add6 217 if (!tipc_link_is_active(l_ptr)) {
b97bf3fd
PL
218 info("Lost standby link <%s> on network plane %c\n",
219 l_ptr->name, l_ptr->b_ptr->net_plane);
220 return;
221 }
222 info("Lost link <%s> on network plane %c\n",
223 l_ptr->name, l_ptr->b_ptr->net_plane);
224
225 active = &n_ptr->active_links[0];
226 if (active[0] == l_ptr)
227 active[0] = active[1];
228 if (active[1] == l_ptr)
229 active[1] = active[0];
230 if (active[0] == l_ptr)
231 node_select_active_links(n_ptr);
c4307285 232 if (tipc_node_is_up(n_ptr))
4323add6 233 tipc_link_changeover(l_ptr);
c4307285 234 else
b97bf3fd
PL
235 node_lost_contact(n_ptr);
236}
237
6c00055a 238int tipc_node_has_active_links(struct tipc_node *n_ptr)
b97bf3fd 239{
76ae0d71 240 return n_ptr->active_links[0] != NULL;
b97bf3fd
PL
241}
242
6c00055a 243int tipc_node_has_redundant_links(struct tipc_node *n_ptr)
b97bf3fd 244{
a02cec21 245 return n_ptr->working_links > 1;
b97bf3fd
PL
246}
247
6c00055a 248static int tipc_node_has_active_routes(struct tipc_node *n_ptr)
b97bf3fd 249{
a02cec21 250 return n_ptr && (n_ptr->last_router >= 0);
b97bf3fd
PL
251}
252
6c00055a 253int tipc_node_is_up(struct tipc_node *n_ptr)
b97bf3fd 254{
a02cec21 255 return tipc_node_has_active_links(n_ptr) || tipc_node_has_active_routes(n_ptr);
b97bf3fd
PL
256}
257
6c00055a 258struct tipc_node *tipc_node_attach_link(struct link *l_ptr)
b97bf3fd 259{
6c00055a 260 struct tipc_node *n_ptr = tipc_node_find(l_ptr->addr);
b97bf3fd
PL
261
262 if (!n_ptr)
4323add6 263 n_ptr = tipc_node_create(l_ptr->addr);
c4307285 264 if (n_ptr) {
b97bf3fd
PL
265 u32 bearer_id = l_ptr->b_ptr->identity;
266 char addr_string[16];
267
c4307285 268 if (n_ptr->link_cnt >= 2) {
c4307285 269 err("Attempt to create third link to %s\n",
c68ca7b7 270 tipc_addr_string_fill(addr_string, n_ptr->addr));
c4307285
YH
271 return NULL;
272 }
273
274 if (!n_ptr->links[bearer_id]) {
275 n_ptr->links[bearer_id] = l_ptr;
276 tipc_net.zones[tipc_zone(l_ptr->addr)]->links++;
277 n_ptr->link_cnt++;
278 return n_ptr;
279 }
a570f095 280 err("Attempt to establish second link on <%s> to %s\n",
c4307285 281 l_ptr->b_ptr->publ.name,
c68ca7b7 282 tipc_addr_string_fill(addr_string, l_ptr->addr));
c4307285 283 }
1fc54d8f 284 return NULL;
b97bf3fd
PL
285}
286
6c00055a 287void tipc_node_detach_link(struct tipc_node *n_ptr, struct link *l_ptr)
b97bf3fd 288{
1fc54d8f 289 n_ptr->links[l_ptr->b_ptr->identity] = NULL;
4323add6 290 tipc_net.zones[tipc_zone(l_ptr->addr)]->links--;
b97bf3fd
PL
291 n_ptr->link_cnt--;
292}
293
294/*
295 * Routing table management - five cases to handle:
296 *
297 * 1: A link towards a zone/cluster external node comes up.
c4307285
YH
298 * => Send a multicast message updating routing tables of all
299 * system nodes within own cluster that the new destination
300 * can be reached via this node.
b97bf3fd
PL
301 * (node.establishedContact()=>cluster.multicastNewRoute())
302 *
303 * 2: A link towards a slave node comes up.
c4307285
YH
304 * => Send a multicast message updating routing tables of all
305 * system nodes within own cluster that the new destination
306 * can be reached via this node.
b97bf3fd 307 * (node.establishedContact()=>cluster.multicastNewRoute())
c4307285 308 * => Send a message to the slave node about existence
b97bf3fd
PL
309 * of all system nodes within cluster:
310 * (node.establishedContact()=>cluster.sendLocalRoutes())
311 *
312 * 3: A new cluster local system node becomes available.
313 * => Send message(s) to this particular node containing
314 * information about all cluster external and slave
315 * nodes which can be reached via this node.
316 * (node.establishedContact()==>network.sendExternalRoutes())
317 * (node.establishedContact()==>network.sendSlaveRoutes())
c4307285 318 * => Send messages to all directly connected slave nodes
b97bf3fd
PL
319 * containing information about the existence of the new node
320 * (node.establishedContact()=>cluster.multicastNewRoute())
c4307285 321 *
b97bf3fd
PL
322 * 4: The link towards a zone/cluster external node or slave
323 * node goes down.
c4307285 324 * => Send a multcast message updating routing tables of all
b97bf3fd
PL
325 * nodes within cluster that the new destination can not any
326 * longer be reached via this node.
327 * (node.lostAllLinks()=>cluster.bcastLostRoute())
328 *
329 * 5: A cluster local system node becomes unavailable.
330 * => Remove all references to this node from the local
331 * routing tables. Note: This is a completely node
332 * local operation.
333 * (node.lostAllLinks()=>network.removeAsRouter())
c4307285 334 * => Send messages to all directly connected slave nodes
b97bf3fd
PL
335 * containing information about loss of the node
336 * (node.establishedContact()=>cluster.multicastLostRoute())
337 *
338 */
339
6c00055a 340static void node_established_contact(struct tipc_node *n_ptr)
b97bf3fd
PL
341{
342 struct cluster *c_ptr;
343
344 dbg("node_established_contact:-> %x\n", n_ptr->addr);
c4307285 345 if (!tipc_node_has_active_routes(n_ptr) && in_own_cluster(n_ptr->addr)) {
4323add6 346 tipc_k_signal((Handler)tipc_named_node_up, n_ptr->addr);
b97bf3fd
PL
347 }
348
c4307285
YH
349 /* Syncronize broadcast acks */
350 n_ptr->bclink.acked = tipc_bclink_get_last_sent();
b97bf3fd
PL
351
352 if (is_slave(tipc_own_addr))
353 return;
354 if (!in_own_cluster(n_ptr->addr)) {
355 /* Usage case 1 (see above) */
4323add6 356 c_ptr = tipc_cltr_find(tipc_own_addr);
b97bf3fd 357 if (!c_ptr)
4323add6 358 c_ptr = tipc_cltr_create(tipc_own_addr);
c4307285
YH
359 if (c_ptr)
360 tipc_cltr_bcast_new_route(c_ptr, n_ptr->addr, 1,
4323add6 361 tipc_max_nodes);
b97bf3fd 362 return;
c4307285 363 }
b97bf3fd
PL
364
365 c_ptr = n_ptr->owner;
366 if (is_slave(n_ptr->addr)) {
367 /* Usage case 2 (see above) */
4323add6
PL
368 tipc_cltr_bcast_new_route(c_ptr, n_ptr->addr, 1, tipc_max_nodes);
369 tipc_cltr_send_local_routes(c_ptr, n_ptr->addr);
b97bf3fd
PL
370 return;
371 }
372
373 if (n_ptr->bclink.supported) {
4323add6 374 tipc_nmap_add(&tipc_cltr_bcast_nodes, n_ptr->addr);
b97bf3fd
PL
375 if (n_ptr->addr < tipc_own_addr)
376 tipc_own_tag++;
377 }
378
379 /* Case 3 (see above) */
4323add6
PL
380 tipc_net_send_external_routes(n_ptr->addr);
381 tipc_cltr_send_slave_routes(c_ptr, n_ptr->addr);
382 tipc_cltr_bcast_new_route(c_ptr, n_ptr->addr, LOWEST_SLAVE,
383 tipc_highest_allowed_slave);
b97bf3fd
PL
384}
385
5a68d5ee
AS
386static void node_cleanup_finished(unsigned long node_addr)
387{
388 struct tipc_node *n_ptr;
389
390 read_lock_bh(&tipc_net_lock);
391 n_ptr = tipc_node_find(node_addr);
392 if (n_ptr) {
393 tipc_node_lock(n_ptr);
394 n_ptr->cleanup_required = 0;
395 tipc_node_unlock(n_ptr);
396 }
397 read_unlock_bh(&tipc_net_lock);
398}
399
6c00055a 400static void node_lost_contact(struct tipc_node *n_ptr)
b97bf3fd
PL
401{
402 struct cluster *c_ptr;
6c00055a 403 struct tipc_node_subscr *ns, *tns;
b97bf3fd
PL
404 char addr_string[16];
405 u32 i;
406
c4307285
YH
407 /* Clean up broadcast reception remains */
408 n_ptr->bclink.gap_after = n_ptr->bclink.gap_to = 0;
409 while (n_ptr->bclink.deferred_head) {
410 struct sk_buff* buf = n_ptr->bclink.deferred_head;
411 n_ptr->bclink.deferred_head = buf->next;
412 buf_discard(buf);
413 }
414 if (n_ptr->bclink.defragm) {
415 buf_discard(n_ptr->bclink.defragm);
416 n_ptr->bclink.defragm = NULL;
417 }
418 if (in_own_cluster(n_ptr->addr) && n_ptr->bclink.supported) {
419 tipc_bclink_acknowledge(n_ptr, mod(n_ptr->bclink.acked + 10000));
420 }
421
422 /* Update routing tables */
b97bf3fd 423 if (is_slave(tipc_own_addr)) {
4323add6 424 tipc_net_remove_as_router(n_ptr->addr);
b97bf3fd 425 } else {
c4307285 426 if (!in_own_cluster(n_ptr->addr)) {
b97bf3fd 427 /* Case 4 (see above) */
4323add6
PL
428 c_ptr = tipc_cltr_find(tipc_own_addr);
429 tipc_cltr_bcast_lost_route(c_ptr, n_ptr->addr, 1,
430 tipc_max_nodes);
b97bf3fd
PL
431 } else {
432 /* Case 5 (see above) */
4323add6 433 c_ptr = tipc_cltr_find(n_ptr->addr);
b97bf3fd 434 if (is_slave(n_ptr->addr)) {
4323add6
PL
435 tipc_cltr_bcast_lost_route(c_ptr, n_ptr->addr, 1,
436 tipc_max_nodes);
b97bf3fd
PL
437 } else {
438 if (n_ptr->bclink.supported) {
c4307285 439 tipc_nmap_remove(&tipc_cltr_bcast_nodes,
4323add6 440 n_ptr->addr);
b97bf3fd
PL
441 if (n_ptr->addr < tipc_own_addr)
442 tipc_own_tag--;
443 }
4323add6
PL
444 tipc_net_remove_as_router(n_ptr->addr);
445 tipc_cltr_bcast_lost_route(c_ptr, n_ptr->addr,
446 LOWEST_SLAVE,
447 tipc_highest_allowed_slave);
b97bf3fd
PL
448 }
449 }
450 }
4323add6 451 if (tipc_node_has_active_routes(n_ptr))
b97bf3fd
PL
452 return;
453
c4307285 454 info("Lost contact with %s\n",
c68ca7b7 455 tipc_addr_string_fill(addr_string, n_ptr->addr));
b97bf3fd
PL
456
457 /* Abort link changeover */
458 for (i = 0; i < MAX_BEARERS; i++) {
459 struct link *l_ptr = n_ptr->links[i];
c4307285 460 if (!l_ptr)
b97bf3fd
PL
461 continue;
462 l_ptr->reset_checkpoint = l_ptr->next_in_no;
463 l_ptr->exp_msg_count = 0;
4323add6 464 tipc_link_reset_fragments(l_ptr);
b97bf3fd
PL
465 }
466
467 /* Notify subscribers */
468 list_for_each_entry_safe(ns, tns, &n_ptr->nsub, nodesub_list) {
c4307285 469 ns->node = NULL;
b97bf3fd 470 list_del_init(&ns->nodesub_list);
4323add6
PL
471 tipc_k_signal((Handler)ns->handle_node_down,
472 (unsigned long)ns->usr_handle);
b97bf3fd 473 }
5a68d5ee
AS
474
475 /* Prevent re-contact with node until all cleanup is done */
476
477 n_ptr->cleanup_required = 1;
478 tipc_k_signal((Handler)node_cleanup_finished, n_ptr->addr);
b97bf3fd
PL
479}
480
481/**
4323add6 482 * tipc_node_select_next_hop - find the next-hop node for a message
c4307285 483 *
b97bf3fd
PL
484 * Called by when cluster local lookup has failed.
485 */
486
6c00055a 487struct tipc_node *tipc_node_select_next_hop(u32 addr, u32 selector)
b97bf3fd 488{
6c00055a 489 struct tipc_node *n_ptr;
b97bf3fd
PL
490 u32 router_addr;
491
c4307285
YH
492 if (!tipc_addr_domain_valid(addr))
493 return NULL;
b97bf3fd
PL
494
495 /* Look for direct link to destination processsor */
4323add6
PL
496 n_ptr = tipc_node_find(addr);
497 if (n_ptr && tipc_node_has_active_links(n_ptr))
c4307285 498 return n_ptr;
b97bf3fd
PL
499
500 /* Cluster local system nodes *must* have direct links */
501 if (!is_slave(addr) && in_own_cluster(addr))
1fc54d8f 502 return NULL;
b97bf3fd
PL
503
504 /* Look for cluster local router with direct link to node */
4323add6 505 router_addr = tipc_node_select_router(n_ptr, selector);
c4307285
YH
506 if (router_addr)
507 return tipc_node_select(router_addr, selector);
b97bf3fd 508
c4307285 509 /* Slave nodes can only be accessed within own cluster via a
b97bf3fd
PL
510 known router with direct link -- if no router was found,give up */
511 if (is_slave(addr))
1fc54d8f 512 return NULL;
b97bf3fd
PL
513
514 /* Inter zone/cluster -- find any direct link to remote cluster */
515 addr = tipc_addr(tipc_zone(addr), tipc_cluster(addr), 0);
4323add6
PL
516 n_ptr = tipc_net_select_remote_node(addr, selector);
517 if (n_ptr && tipc_node_has_active_links(n_ptr))
c4307285 518 return n_ptr;
b97bf3fd
PL
519
520 /* Last resort -- look for any router to anywhere in remote zone */
4323add6 521 router_addr = tipc_net_select_router(addr, selector);
c4307285
YH
522 if (router_addr)
523 return tipc_node_select(router_addr, selector);
b97bf3fd 524
c4307285 525 return NULL;
b97bf3fd
PL
526}
527
528/**
4323add6 529 * tipc_node_select_router - select router to reach specified node
c4307285
YH
530 *
531 * Uses a deterministic and fair algorithm for selecting router node.
b97bf3fd
PL
532 */
533
6c00055a 534u32 tipc_node_select_router(struct tipc_node *n_ptr, u32 ref)
b97bf3fd
PL
535{
536 u32 ulim;
537 u32 mask;
538 u32 start;
539 u32 r;
540
c4307285
YH
541 if (!n_ptr)
542 return 0;
b97bf3fd
PL
543
544 if (n_ptr->last_router < 0)
545 return 0;
546 ulim = ((n_ptr->last_router + 1) * 32) - 1;
547
548 /* Start entry must be random */
549 mask = tipc_max_nodes;
550 while (mask > ulim)
551 mask >>= 1;
552 start = ref & mask;
553 r = start;
554
555 /* Lookup upwards with wrap-around */
556 do {
557 if (((n_ptr->routers[r / 32]) >> (r % 32)) & 1)
558 break;
559 } while (++r <= ulim);
560 if (r > ulim) {
561 r = 1;
562 do {
563 if (((n_ptr->routers[r / 32]) >> (r % 32)) & 1)
564 break;
565 } while (++r < start);
566 assert(r != start);
567 }
568 assert(r && (r <= ulim));
569 return tipc_addr(own_zone(), own_cluster(), r);
570}
571
6c00055a 572void tipc_node_add_router(struct tipc_node *n_ptr, u32 router)
b97bf3fd
PL
573{
574 u32 r_num = tipc_node(router);
575
c4307285 576 n_ptr->routers[r_num / 32] =
b97bf3fd
PL
577 ((1 << (r_num % 32)) | n_ptr->routers[r_num / 32]);
578 n_ptr->last_router = tipc_max_nodes / 32;
c4307285 579 while ((--n_ptr->last_router >= 0) &&
b97bf3fd
PL
580 !n_ptr->routers[n_ptr->last_router]);
581}
582
6c00055a 583void tipc_node_remove_router(struct tipc_node *n_ptr, u32 router)
b97bf3fd
PL
584{
585 u32 r_num = tipc_node(router);
586
587 if (n_ptr->last_router < 0)
588 return; /* No routes */
589
590 n_ptr->routers[r_num / 32] =
591 ((~(1 << (r_num % 32))) & (n_ptr->routers[r_num / 32]));
592 n_ptr->last_router = tipc_max_nodes / 32;
c4307285 593 while ((--n_ptr->last_router >= 0) &&
b97bf3fd
PL
594 !n_ptr->routers[n_ptr->last_router]);
595
4323add6 596 if (!tipc_node_is_up(n_ptr))
b97bf3fd
PL
597 node_lost_contact(n_ptr);
598}
599
600#if 0
6c00055a 601void node_print(struct print_buf *buf, struct tipc_node *n_ptr, char *str)
b97bf3fd
PL
602{
603 u32 i;
604
605 tipc_printf(buf, "\n\n%s", str);
606 for (i = 0; i < MAX_BEARERS; i++) {
c4307285 607 if (!n_ptr->links[i])
b97bf3fd
PL
608 continue;
609 tipc_printf(buf, "Links[%u]: %x, ", i, n_ptr->links[i]);
610 }
611 tipc_printf(buf, "Active links: [%x,%x]\n",
612 n_ptr->active_links[0], n_ptr->active_links[1]);
613}
614#endif
615
616u32 tipc_available_nodes(const u32 domain)
617{
6c00055a 618 struct tipc_node *n_ptr;
b97bf3fd
PL
619 u32 cnt = 0;
620
1aad72d6 621 read_lock_bh(&tipc_net_lock);
4323add6 622 for (n_ptr = tipc_nodes; n_ptr; n_ptr = n_ptr->next) {
c68ca7b7 623 if (!tipc_in_scope(domain, n_ptr->addr))
b97bf3fd 624 continue;
4323add6 625 if (tipc_node_is_up(n_ptr))
b97bf3fd
PL
626 cnt++;
627 }
1aad72d6 628 read_unlock_bh(&tipc_net_lock);
b97bf3fd
PL
629 return cnt;
630}
631
4323add6 632struct sk_buff *tipc_node_get_nodes(const void *req_tlv_area, int req_tlv_space)
b97bf3fd
PL
633{
634 u32 domain;
635 struct sk_buff *buf;
6c00055a 636 struct tipc_node *n_ptr;
c4307285 637 struct tipc_node_info node_info;
ea13847b 638 u32 payload_size;
b97bf3fd
PL
639
640 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NET_ADDR))
4323add6 641 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
b97bf3fd 642
3e6c8cd5 643 domain = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
4323add6
PL
644 if (!tipc_addr_domain_valid(domain))
645 return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
646 " (network address)");
b97bf3fd 647
1aad72d6
AS
648 read_lock_bh(&tipc_net_lock);
649 if (!tipc_nodes) {
650 read_unlock_bh(&tipc_net_lock);
c4307285 651 return tipc_cfg_reply_none();
1aad72d6 652 }
b97bf3fd 653
c4307285 654 /* For now, get space for all other nodes
b97bf3fd
PL
655 (will need to modify this when slave nodes are supported */
656
ea13847b 657 payload_size = TLV_SPACE(sizeof(node_info)) * (tipc_max_nodes - 1);
1aad72d6
AS
658 if (payload_size > 32768u) {
659 read_unlock_bh(&tipc_net_lock);
ea13847b
AS
660 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
661 " (too many nodes)");
1aad72d6 662 }
ea13847b 663 buf = tipc_cfg_reply_alloc(payload_size);
1aad72d6
AS
664 if (!buf) {
665 read_unlock_bh(&tipc_net_lock);
b97bf3fd 666 return NULL;
1aad72d6 667 }
b97bf3fd
PL
668
669 /* Add TLVs for all nodes in scope */
670
4323add6 671 for (n_ptr = tipc_nodes; n_ptr; n_ptr = n_ptr->next) {
c68ca7b7 672 if (!tipc_in_scope(domain, n_ptr->addr))
b97bf3fd 673 continue;
c4307285
YH
674 node_info.addr = htonl(n_ptr->addr);
675 node_info.up = htonl(tipc_node_is_up(n_ptr));
676 tipc_cfg_append_tlv(buf, TIPC_TLV_NODE_INFO,
4323add6 677 &node_info, sizeof(node_info));
b97bf3fd
PL
678 }
679
1aad72d6 680 read_unlock_bh(&tipc_net_lock);
b97bf3fd
PL
681 return buf;
682}
683
4323add6 684struct sk_buff *tipc_node_get_links(const void *req_tlv_area, int req_tlv_space)
b97bf3fd
PL
685{
686 u32 domain;
687 struct sk_buff *buf;
6c00055a 688 struct tipc_node *n_ptr;
c4307285 689 struct tipc_link_info link_info;
ea13847b 690 u32 payload_size;
b97bf3fd
PL
691
692 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NET_ADDR))
4323add6 693 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
b97bf3fd 694
3e6c8cd5 695 domain = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
4323add6
PL
696 if (!tipc_addr_domain_valid(domain))
697 return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
698 " (network address)");
b97bf3fd 699
c4307285
YH
700 if (tipc_mode != TIPC_NET_MODE)
701 return tipc_cfg_reply_none();
702
1aad72d6
AS
703 read_lock_bh(&tipc_net_lock);
704
ea13847b
AS
705 /* Get space for all unicast links + multicast link */
706
707 payload_size = TLV_SPACE(sizeof(link_info)) *
708 (tipc_net.zones[tipc_zone(tipc_own_addr)]->links + 1);
1aad72d6
AS
709 if (payload_size > 32768u) {
710 read_unlock_bh(&tipc_net_lock);
ea13847b
AS
711 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
712 " (too many links)");
1aad72d6 713 }
ea13847b 714 buf = tipc_cfg_reply_alloc(payload_size);
1aad72d6
AS
715 if (!buf) {
716 read_unlock_bh(&tipc_net_lock);
b97bf3fd 717 return NULL;
1aad72d6 718 }
b97bf3fd
PL
719
720 /* Add TLV for broadcast link */
721
c4307285
YH
722 link_info.dest = htonl(tipc_own_addr & 0xfffff00);
723 link_info.up = htonl(1);
4b704d59 724 strlcpy(link_info.str, tipc_bclink_name, TIPC_MAX_LINK_NAME);
4323add6 725 tipc_cfg_append_tlv(buf, TIPC_TLV_LINK_INFO, &link_info, sizeof(link_info));
b97bf3fd
PL
726
727 /* Add TLVs for any other links in scope */
728
4323add6 729 for (n_ptr = tipc_nodes; n_ptr; n_ptr = n_ptr->next) {
c4307285 730 u32 i;
b97bf3fd 731
c68ca7b7 732 if (!tipc_in_scope(domain, n_ptr->addr))
b97bf3fd 733 continue;
1aad72d6 734 tipc_node_lock(n_ptr);
c4307285
YH
735 for (i = 0; i < MAX_BEARERS; i++) {
736 if (!n_ptr->links[i])
737 continue;
738 link_info.dest = htonl(n_ptr->addr);
739 link_info.up = htonl(tipc_link_is_up(n_ptr->links[i]));
740 strcpy(link_info.str, n_ptr->links[i]->name);
741 tipc_cfg_append_tlv(buf, TIPC_TLV_LINK_INFO,
4323add6 742 &link_info, sizeof(link_info));
c4307285 743 }
1aad72d6 744 tipc_node_unlock(n_ptr);
b97bf3fd
PL
745 }
746
1aad72d6 747 read_unlock_bh(&tipc_net_lock);
b97bf3fd
PL
748 return buf;
749}