blob: b54462bd98d7242a05615a6d7d5e290e9db997b2 [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
2 * net/tipc/node.c: TIPC node management routines
3 *
Per Liden593a5f22006-01-11 19:14:19 +01004 * Copyright (c) 2000-2006, Ericsson AB
Per Lidenb97bf3f2006-01-02 19:04:38 +01005 * Copyright (c) 2005, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * All rights reserved.
7 *
Per Liden9ea1fd32006-01-11 13:30:43 +01008 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +01009 * modification, are permitted provided that the following conditions are met:
10 *
Per Liden9ea1fd32006-01-11 13:30:43 +010011 * 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.
Per Lidenb97bf3f2006-01-02 19:04:38 +010019 *
Per Liden9ea1fd32006-01-11 13:30:43 +010020 * 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
Per Lidenb97bf3f2006-01-02 19:04:38 +010034 * 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"
Per Lidenb97bf3f2006-01-02 19:04:38 +010048
49void node_print(struct print_buf *buf, struct node *n_ptr, char *str);
50static void node_lost_contact(struct node *n_ptr);
51static void node_established_contact(struct node *n_ptr);
52
Per Liden4323add2006-01-18 00:38:21 +010053struct node *tipc_nodes = NULL; /* sorted list of nodes within cluster */
Per Lidenb97bf3f2006-01-02 19:04:38 +010054
55u32 tipc_own_tag = 0;
56
Per Liden4323add2006-01-18 00:38:21 +010057struct node *tipc_node_create(u32 addr)
Per Lidenb97bf3f2006-01-02 19:04:38 +010058{
59 struct cluster *c_ptr;
60 struct node *n_ptr;
61 struct node **curr_node;
62
63 n_ptr = kmalloc(sizeof(*n_ptr),GFP_ATOMIC);
64 if (n_ptr != NULL) {
65 memset(n_ptr, 0, sizeof(*n_ptr));
66 n_ptr->addr = addr;
67 n_ptr->lock = SPIN_LOCK_UNLOCKED;
68 INIT_LIST_HEAD(&n_ptr->nsub);
69
Per Liden4323add2006-01-18 00:38:21 +010070 c_ptr = tipc_cltr_find(addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +010071 if (c_ptr == NULL)
Per Liden4323add2006-01-18 00:38:21 +010072 c_ptr = tipc_cltr_create(addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +010073 if (c_ptr != NULL) {
74 n_ptr->owner = c_ptr;
Per Liden4323add2006-01-18 00:38:21 +010075 tipc_cltr_attach_node(c_ptr, n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +010076 n_ptr->last_router = -1;
77
78 /* Insert node into ordered list */
Per Liden4323add2006-01-18 00:38:21 +010079 for (curr_node = &tipc_nodes; *curr_node;
Per Lidenb97bf3f2006-01-02 19:04:38 +010080 curr_node = &(*curr_node)->next) {
81 if (addr < (*curr_node)->addr) {
82 n_ptr->next = *curr_node;
83 break;
84 }
85 }
86 (*curr_node) = n_ptr;
87 } else {
88 kfree(n_ptr);
89 n_ptr = NULL;
90 }
91 }
92 return n_ptr;
93}
94
Per Liden4323add2006-01-18 00:38:21 +010095void tipc_node_delete(struct node *n_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +010096{
97 if (!n_ptr)
98 return;
99
100#if 0
Per Liden4323add2006-01-18 00:38:21 +0100101 /* Not needed because links are already deleted via tipc_bearer_stop() */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100102
103 u32 l_num;
104
105 for (l_num = 0; l_num < MAX_BEARERS; l_num++) {
106 link_delete(n_ptr->links[l_num]);
107 }
108#endif
109
110 dbg("node %x deleted\n", n_ptr->addr);
111 kfree(n_ptr);
112}
113
114
115/**
Per Liden4323add2006-01-18 00:38:21 +0100116 * tipc_node_link_up - handle addition of link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100117 *
118 * Link becomes active (alone or shared) or standby, depending on its priority.
119 */
120
Per Liden4323add2006-01-18 00:38:21 +0100121void tipc_node_link_up(struct node *n_ptr, struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100122{
123 struct link **active = &n_ptr->active_links[0];
124
125 info("Established link <%s> on network plane %c\n",
126 l_ptr->name, l_ptr->b_ptr->net_plane);
127
128 if (!active[0]) {
129 dbg(" link %x into %x/%x\n", l_ptr, &active[0], &active[1]);
130 active[0] = active[1] = l_ptr;
131 node_established_contact(n_ptr);
132 return;
133 }
134 if (l_ptr->priority < active[0]->priority) {
135 info("Link is standby\n");
136 return;
137 }
Per Liden4323add2006-01-18 00:38:21 +0100138 tipc_link_send_duplicate(active[0], l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100139 if (l_ptr->priority == active[0]->priority) {
140 active[0] = l_ptr;
141 return;
142 }
143 info("Link <%s> on network plane %c becomes standby\n",
144 active[0]->name, active[0]->b_ptr->net_plane);
145 active[0] = active[1] = l_ptr;
146}
147
148/**
149 * node_select_active_links - select active link
150 */
151
152static void node_select_active_links(struct node *n_ptr)
153{
154 struct link **active = &n_ptr->active_links[0];
155 u32 i;
156 u32 highest_prio = 0;
157
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800158 active[0] = active[1] = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100159
160 for (i = 0; i < MAX_BEARERS; i++) {
161 struct link *l_ptr = n_ptr->links[i];
162
Per Liden4323add2006-01-18 00:38:21 +0100163 if (!l_ptr || !tipc_link_is_up(l_ptr) ||
Per Lidenb97bf3f2006-01-02 19:04:38 +0100164 (l_ptr->priority < highest_prio))
165 continue;
166
167 if (l_ptr->priority > highest_prio) {
168 highest_prio = l_ptr->priority;
169 active[0] = active[1] = l_ptr;
170 } else {
171 active[1] = l_ptr;
172 }
173 }
174}
175
176/**
Per Liden4323add2006-01-18 00:38:21 +0100177 * tipc_node_link_down - handle loss of link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100178 */
179
Per Liden4323add2006-01-18 00:38:21 +0100180void tipc_node_link_down(struct node *n_ptr, struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100181{
182 struct link **active;
183
Per Liden4323add2006-01-18 00:38:21 +0100184 if (!tipc_link_is_active(l_ptr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100185 info("Lost standby link <%s> on network plane %c\n",
186 l_ptr->name, l_ptr->b_ptr->net_plane);
187 return;
188 }
189 info("Lost link <%s> on network plane %c\n",
190 l_ptr->name, l_ptr->b_ptr->net_plane);
191
192 active = &n_ptr->active_links[0];
193 if (active[0] == l_ptr)
194 active[0] = active[1];
195 if (active[1] == l_ptr)
196 active[1] = active[0];
197 if (active[0] == l_ptr)
198 node_select_active_links(n_ptr);
Per Liden4323add2006-01-18 00:38:21 +0100199 if (tipc_node_is_up(n_ptr))
200 tipc_link_changeover(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100201 else
202 node_lost_contact(n_ptr);
203}
204
Per Liden4323add2006-01-18 00:38:21 +0100205int tipc_node_has_active_links(struct node *n_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100206{
207 return (n_ptr &&
208 ((n_ptr->active_links[0]) || (n_ptr->active_links[1])));
209}
210
Per Liden4323add2006-01-18 00:38:21 +0100211int tipc_node_has_redundant_links(struct node *n_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100212{
Per Liden4323add2006-01-18 00:38:21 +0100213 return (tipc_node_has_active_links(n_ptr) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +0100214 (n_ptr->active_links[0] != n_ptr->active_links[1]));
215}
216
Adrian Bunk988f0882006-03-20 22:37:52 -0800217static int tipc_node_has_active_routes(struct node *n_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100218{
219 return (n_ptr && (n_ptr->last_router >= 0));
220}
221
Per Liden4323add2006-01-18 00:38:21 +0100222int tipc_node_is_up(struct node *n_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100223{
Per Liden4323add2006-01-18 00:38:21 +0100224 return (tipc_node_has_active_links(n_ptr) || tipc_node_has_active_routes(n_ptr));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100225}
226
Per Liden4323add2006-01-18 00:38:21 +0100227struct node *tipc_node_attach_link(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100228{
Per Liden4323add2006-01-18 00:38:21 +0100229 struct node *n_ptr = tipc_node_find(l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100230
231 if (!n_ptr)
Per Liden4323add2006-01-18 00:38:21 +0100232 n_ptr = tipc_node_create(l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100233 if (n_ptr) {
234 u32 bearer_id = l_ptr->b_ptr->identity;
235 char addr_string[16];
236
Per Lidenb97bf3f2006-01-02 19:04:38 +0100237 if (n_ptr->link_cnt >= 2) {
238 char addr_string[16];
239
240 err("Attempt to create third link to %s\n",
241 addr_string_fill(addr_string, n_ptr->addr));
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800242 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100243 }
244
245 if (!n_ptr->links[bearer_id]) {
246 n_ptr->links[bearer_id] = l_ptr;
Per Liden4323add2006-01-18 00:38:21 +0100247 tipc_net.zones[tipc_zone(l_ptr->addr)]->links++;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100248 n_ptr->link_cnt++;
249 return n_ptr;
250 }
251 err("Attempt to establish second link on <%s> to <%s> \n",
252 l_ptr->b_ptr->publ.name,
253 addr_string_fill(addr_string, l_ptr->addr));
254 }
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800255 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100256}
257
Per Liden4323add2006-01-18 00:38:21 +0100258void tipc_node_detach_link(struct node *n_ptr, struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100259{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800260 n_ptr->links[l_ptr->b_ptr->identity] = NULL;
Per Liden4323add2006-01-18 00:38:21 +0100261 tipc_net.zones[tipc_zone(l_ptr->addr)]->links--;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100262 n_ptr->link_cnt--;
263}
264
265/*
266 * Routing table management - five cases to handle:
267 *
268 * 1: A link towards a zone/cluster external node comes up.
269 * => Send a multicast message updating routing tables of all
270 * system nodes within own cluster that the new destination
271 * can be reached via this node.
272 * (node.establishedContact()=>cluster.multicastNewRoute())
273 *
274 * 2: A link towards a slave node comes up.
275 * => Send a multicast message updating routing tables of all
276 * system nodes within own cluster that the new destination
277 * can be reached via this node.
278 * (node.establishedContact()=>cluster.multicastNewRoute())
279 * => Send a message to the slave node about existence
280 * of all system nodes within cluster:
281 * (node.establishedContact()=>cluster.sendLocalRoutes())
282 *
283 * 3: A new cluster local system node becomes available.
284 * => Send message(s) to this particular node containing
285 * information about all cluster external and slave
286 * nodes which can be reached via this node.
287 * (node.establishedContact()==>network.sendExternalRoutes())
288 * (node.establishedContact()==>network.sendSlaveRoutes())
289 * => Send messages to all directly connected slave nodes
290 * containing information about the existence of the new node
291 * (node.establishedContact()=>cluster.multicastNewRoute())
292 *
293 * 4: The link towards a zone/cluster external node or slave
294 * node goes down.
295 * => Send a multcast message updating routing tables of all
296 * nodes within cluster that the new destination can not any
297 * longer be reached via this node.
298 * (node.lostAllLinks()=>cluster.bcastLostRoute())
299 *
300 * 5: A cluster local system node becomes unavailable.
301 * => Remove all references to this node from the local
302 * routing tables. Note: This is a completely node
303 * local operation.
304 * (node.lostAllLinks()=>network.removeAsRouter())
305 * => Send messages to all directly connected slave nodes
306 * containing information about loss of the node
307 * (node.establishedContact()=>cluster.multicastLostRoute())
308 *
309 */
310
311static void node_established_contact(struct node *n_ptr)
312{
313 struct cluster *c_ptr;
314
315 dbg("node_established_contact:-> %x\n", n_ptr->addr);
Allan Stephensf1310722006-06-25 23:51:37 -0700316 if (!tipc_node_has_active_routes(n_ptr) && in_own_cluster(n_ptr->addr)) {
Per Liden4323add2006-01-18 00:38:21 +0100317 tipc_k_signal((Handler)tipc_named_node_up, n_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100318 }
319
320 /* Syncronize broadcast acks */
Per Liden4323add2006-01-18 00:38:21 +0100321 n_ptr->bclink.acked = tipc_bclink_get_last_sent();
Per Lidenb97bf3f2006-01-02 19:04:38 +0100322
323 if (is_slave(tipc_own_addr))
324 return;
325 if (!in_own_cluster(n_ptr->addr)) {
326 /* Usage case 1 (see above) */
Per Liden4323add2006-01-18 00:38:21 +0100327 c_ptr = tipc_cltr_find(tipc_own_addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100328 if (!c_ptr)
Per Liden4323add2006-01-18 00:38:21 +0100329 c_ptr = tipc_cltr_create(tipc_own_addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100330 if (c_ptr)
Per Liden4323add2006-01-18 00:38:21 +0100331 tipc_cltr_bcast_new_route(c_ptr, n_ptr->addr, 1,
332 tipc_max_nodes);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100333 return;
334 }
335
336 c_ptr = n_ptr->owner;
337 if (is_slave(n_ptr->addr)) {
338 /* Usage case 2 (see above) */
Per Liden4323add2006-01-18 00:38:21 +0100339 tipc_cltr_bcast_new_route(c_ptr, n_ptr->addr, 1, tipc_max_nodes);
340 tipc_cltr_send_local_routes(c_ptr, n_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100341 return;
342 }
343
344 if (n_ptr->bclink.supported) {
Per Liden4323add2006-01-18 00:38:21 +0100345 tipc_nmap_add(&tipc_cltr_bcast_nodes, n_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100346 if (n_ptr->addr < tipc_own_addr)
347 tipc_own_tag++;
348 }
349
350 /* Case 3 (see above) */
Per Liden4323add2006-01-18 00:38:21 +0100351 tipc_net_send_external_routes(n_ptr->addr);
352 tipc_cltr_send_slave_routes(c_ptr, n_ptr->addr);
353 tipc_cltr_bcast_new_route(c_ptr, n_ptr->addr, LOWEST_SLAVE,
354 tipc_highest_allowed_slave);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100355}
356
357static void node_lost_contact(struct node *n_ptr)
358{
359 struct cluster *c_ptr;
360 struct node_subscr *ns, *tns;
361 char addr_string[16];
362 u32 i;
363
364 /* Clean up broadcast reception remains */
365 n_ptr->bclink.gap_after = n_ptr->bclink.gap_to = 0;
366 while (n_ptr->bclink.deferred_head) {
367 struct sk_buff* buf = n_ptr->bclink.deferred_head;
368 n_ptr->bclink.deferred_head = buf->next;
369 buf_discard(buf);
370 }
371 if (n_ptr->bclink.defragm) {
372 buf_discard(n_ptr->bclink.defragm);
373 n_ptr->bclink.defragm = NULL;
374 }
375 if (in_own_cluster(n_ptr->addr) && n_ptr->bclink.supported) {
Per Liden4323add2006-01-18 00:38:21 +0100376 tipc_bclink_acknowledge(n_ptr, mod(n_ptr->bclink.acked + 10000));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100377 }
378
379 /* Update routing tables */
380 if (is_slave(tipc_own_addr)) {
Per Liden4323add2006-01-18 00:38:21 +0100381 tipc_net_remove_as_router(n_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100382 } else {
383 if (!in_own_cluster(n_ptr->addr)) {
384 /* Case 4 (see above) */
Per Liden4323add2006-01-18 00:38:21 +0100385 c_ptr = tipc_cltr_find(tipc_own_addr);
386 tipc_cltr_bcast_lost_route(c_ptr, n_ptr->addr, 1,
387 tipc_max_nodes);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100388 } else {
389 /* Case 5 (see above) */
Per Liden4323add2006-01-18 00:38:21 +0100390 c_ptr = tipc_cltr_find(n_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100391 if (is_slave(n_ptr->addr)) {
Per Liden4323add2006-01-18 00:38:21 +0100392 tipc_cltr_bcast_lost_route(c_ptr, n_ptr->addr, 1,
393 tipc_max_nodes);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100394 } else {
395 if (n_ptr->bclink.supported) {
Per Liden4323add2006-01-18 00:38:21 +0100396 tipc_nmap_remove(&tipc_cltr_bcast_nodes,
397 n_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100398 if (n_ptr->addr < tipc_own_addr)
399 tipc_own_tag--;
400 }
Per Liden4323add2006-01-18 00:38:21 +0100401 tipc_net_remove_as_router(n_ptr->addr);
402 tipc_cltr_bcast_lost_route(c_ptr, n_ptr->addr,
403 LOWEST_SLAVE,
404 tipc_highest_allowed_slave);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100405 }
406 }
407 }
Per Liden4323add2006-01-18 00:38:21 +0100408 if (tipc_node_has_active_routes(n_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100409 return;
410
411 info("Lost contact with %s\n",
412 addr_string_fill(addr_string, n_ptr->addr));
413
414 /* Abort link changeover */
415 for (i = 0; i < MAX_BEARERS; i++) {
416 struct link *l_ptr = n_ptr->links[i];
417 if (!l_ptr)
418 continue;
419 l_ptr->reset_checkpoint = l_ptr->next_in_no;
420 l_ptr->exp_msg_count = 0;
Per Liden4323add2006-01-18 00:38:21 +0100421 tipc_link_reset_fragments(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100422 }
423
424 /* Notify subscribers */
425 list_for_each_entry_safe(ns, tns, &n_ptr->nsub, nodesub_list) {
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800426 ns->node = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100427 list_del_init(&ns->nodesub_list);
Per Liden4323add2006-01-18 00:38:21 +0100428 tipc_k_signal((Handler)ns->handle_node_down,
429 (unsigned long)ns->usr_handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100430 }
431}
432
433/**
Per Liden4323add2006-01-18 00:38:21 +0100434 * tipc_node_select_next_hop - find the next-hop node for a message
Per Lidenb97bf3f2006-01-02 19:04:38 +0100435 *
436 * Called by when cluster local lookup has failed.
437 */
438
Per Liden4323add2006-01-18 00:38:21 +0100439struct node *tipc_node_select_next_hop(u32 addr, u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100440{
441 struct node *n_ptr;
442 u32 router_addr;
443
Per Liden4323add2006-01-18 00:38:21 +0100444 if (!tipc_addr_domain_valid(addr))
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800445 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100446
447 /* Look for direct link to destination processsor */
Per Liden4323add2006-01-18 00:38:21 +0100448 n_ptr = tipc_node_find(addr);
449 if (n_ptr && tipc_node_has_active_links(n_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100450 return n_ptr;
451
452 /* Cluster local system nodes *must* have direct links */
453 if (!is_slave(addr) && in_own_cluster(addr))
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800454 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100455
456 /* Look for cluster local router with direct link to node */
Per Liden4323add2006-01-18 00:38:21 +0100457 router_addr = tipc_node_select_router(n_ptr, selector);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100458 if (router_addr)
Per Liden4323add2006-01-18 00:38:21 +0100459 return tipc_node_select(router_addr, selector);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100460
461 /* Slave nodes can only be accessed within own cluster via a
462 known router with direct link -- if no router was found,give up */
463 if (is_slave(addr))
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800464 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100465
466 /* Inter zone/cluster -- find any direct link to remote cluster */
467 addr = tipc_addr(tipc_zone(addr), tipc_cluster(addr), 0);
Per Liden4323add2006-01-18 00:38:21 +0100468 n_ptr = tipc_net_select_remote_node(addr, selector);
469 if (n_ptr && tipc_node_has_active_links(n_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100470 return n_ptr;
471
472 /* Last resort -- look for any router to anywhere in remote zone */
Per Liden4323add2006-01-18 00:38:21 +0100473 router_addr = tipc_net_select_router(addr, selector);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100474 if (router_addr)
Per Liden4323add2006-01-18 00:38:21 +0100475 return tipc_node_select(router_addr, selector);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100476
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800477 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100478}
479
480/**
Per Liden4323add2006-01-18 00:38:21 +0100481 * tipc_node_select_router - select router to reach specified node
Per Lidenb97bf3f2006-01-02 19:04:38 +0100482 *
483 * Uses a deterministic and fair algorithm for selecting router node.
484 */
485
Per Liden4323add2006-01-18 00:38:21 +0100486u32 tipc_node_select_router(struct node *n_ptr, u32 ref)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100487{
488 u32 ulim;
489 u32 mask;
490 u32 start;
491 u32 r;
492
493 if (!n_ptr)
494 return 0;
495
496 if (n_ptr->last_router < 0)
497 return 0;
498 ulim = ((n_ptr->last_router + 1) * 32) - 1;
499
500 /* Start entry must be random */
501 mask = tipc_max_nodes;
502 while (mask > ulim)
503 mask >>= 1;
504 start = ref & mask;
505 r = start;
506
507 /* Lookup upwards with wrap-around */
508 do {
509 if (((n_ptr->routers[r / 32]) >> (r % 32)) & 1)
510 break;
511 } while (++r <= ulim);
512 if (r > ulim) {
513 r = 1;
514 do {
515 if (((n_ptr->routers[r / 32]) >> (r % 32)) & 1)
516 break;
517 } while (++r < start);
518 assert(r != start);
519 }
520 assert(r && (r <= ulim));
521 return tipc_addr(own_zone(), own_cluster(), r);
522}
523
Per Liden4323add2006-01-18 00:38:21 +0100524void tipc_node_add_router(struct node *n_ptr, u32 router)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100525{
526 u32 r_num = tipc_node(router);
527
528 n_ptr->routers[r_num / 32] =
529 ((1 << (r_num % 32)) | n_ptr->routers[r_num / 32]);
530 n_ptr->last_router = tipc_max_nodes / 32;
531 while ((--n_ptr->last_router >= 0) &&
532 !n_ptr->routers[n_ptr->last_router]);
533}
534
Per Liden4323add2006-01-18 00:38:21 +0100535void tipc_node_remove_router(struct node *n_ptr, u32 router)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100536{
537 u32 r_num = tipc_node(router);
538
539 if (n_ptr->last_router < 0)
540 return; /* No routes */
541
542 n_ptr->routers[r_num / 32] =
543 ((~(1 << (r_num % 32))) & (n_ptr->routers[r_num / 32]));
544 n_ptr->last_router = tipc_max_nodes / 32;
545 while ((--n_ptr->last_router >= 0) &&
546 !n_ptr->routers[n_ptr->last_router]);
547
Per Liden4323add2006-01-18 00:38:21 +0100548 if (!tipc_node_is_up(n_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100549 node_lost_contact(n_ptr);
550}
551
552#if 0
553void node_print(struct print_buf *buf, struct node *n_ptr, char *str)
554{
555 u32 i;
556
557 tipc_printf(buf, "\n\n%s", str);
558 for (i = 0; i < MAX_BEARERS; i++) {
559 if (!n_ptr->links[i])
560 continue;
561 tipc_printf(buf, "Links[%u]: %x, ", i, n_ptr->links[i]);
562 }
563 tipc_printf(buf, "Active links: [%x,%x]\n",
564 n_ptr->active_links[0], n_ptr->active_links[1]);
565}
566#endif
567
568u32 tipc_available_nodes(const u32 domain)
569{
570 struct node *n_ptr;
571 u32 cnt = 0;
572
Per Liden4323add2006-01-18 00:38:21 +0100573 for (n_ptr = tipc_nodes; n_ptr; n_ptr = n_ptr->next) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100574 if (!in_scope(domain, n_ptr->addr))
575 continue;
Per Liden4323add2006-01-18 00:38:21 +0100576 if (tipc_node_is_up(n_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100577 cnt++;
578 }
579 return cnt;
580}
581
Per Liden4323add2006-01-18 00:38:21 +0100582struct sk_buff *tipc_node_get_nodes(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100583{
584 u32 domain;
585 struct sk_buff *buf;
586 struct node *n_ptr;
587 struct tipc_node_info node_info;
588
589 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NET_ADDR))
Per Liden4323add2006-01-18 00:38:21 +0100590 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100591
592 domain = *(u32 *)TLV_DATA(req_tlv_area);
593 domain = ntohl(domain);
Per Liden4323add2006-01-18 00:38:21 +0100594 if (!tipc_addr_domain_valid(domain))
595 return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
596 " (network address)");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100597
Per Liden4323add2006-01-18 00:38:21 +0100598 if (!tipc_nodes)
599 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +0100600
601 /* For now, get space for all other nodes
602 (will need to modify this when slave nodes are supported */
603
Per Liden4323add2006-01-18 00:38:21 +0100604 buf = tipc_cfg_reply_alloc(TLV_SPACE(sizeof(node_info)) *
605 (tipc_max_nodes - 1));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100606 if (!buf)
607 return NULL;
608
609 /* Add TLVs for all nodes in scope */
610
Per Liden4323add2006-01-18 00:38:21 +0100611 for (n_ptr = tipc_nodes; n_ptr; n_ptr = n_ptr->next) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100612 if (!in_scope(domain, n_ptr->addr))
613 continue;
614 node_info.addr = htonl(n_ptr->addr);
Per Liden4323add2006-01-18 00:38:21 +0100615 node_info.up = htonl(tipc_node_is_up(n_ptr));
616 tipc_cfg_append_tlv(buf, TIPC_TLV_NODE_INFO,
617 &node_info, sizeof(node_info));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100618 }
619
620 return buf;
621}
622
Per Liden4323add2006-01-18 00:38:21 +0100623struct sk_buff *tipc_node_get_links(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100624{
625 u32 domain;
626 struct sk_buff *buf;
627 struct node *n_ptr;
628 struct tipc_link_info link_info;
629
630 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NET_ADDR))
Per Liden4323add2006-01-18 00:38:21 +0100631 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100632
633 domain = *(u32 *)TLV_DATA(req_tlv_area);
634 domain = ntohl(domain);
Per Liden4323add2006-01-18 00:38:21 +0100635 if (!tipc_addr_domain_valid(domain))
636 return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
637 " (network address)");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100638
Per Liden4323add2006-01-18 00:38:21 +0100639 if (!tipc_nodes)
640 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +0100641
642 /* For now, get space for 2 links to all other nodes + bcast link
643 (will need to modify this when slave nodes are supported */
644
Per Liden4323add2006-01-18 00:38:21 +0100645 buf = tipc_cfg_reply_alloc(TLV_SPACE(sizeof(link_info)) *
646 (2 * (tipc_max_nodes - 1) + 1));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100647 if (!buf)
648 return NULL;
649
650 /* Add TLV for broadcast link */
651
652 link_info.dest = tipc_own_addr & 0xfffff00;
653 link_info.dest = htonl(link_info.dest);
654 link_info.up = htonl(1);
Per Liden4323add2006-01-18 00:38:21 +0100655 sprintf(link_info.str, tipc_bclink_name);
656 tipc_cfg_append_tlv(buf, TIPC_TLV_LINK_INFO, &link_info, sizeof(link_info));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100657
658 /* Add TLVs for any other links in scope */
659
Per Liden4323add2006-01-18 00:38:21 +0100660 for (n_ptr = tipc_nodes; n_ptr; n_ptr = n_ptr->next) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100661 u32 i;
662
663 if (!in_scope(domain, n_ptr->addr))
664 continue;
665 for (i = 0; i < MAX_BEARERS; i++) {
666 if (!n_ptr->links[i])
667 continue;
668 link_info.dest = htonl(n_ptr->addr);
Per Liden4323add2006-01-18 00:38:21 +0100669 link_info.up = htonl(tipc_link_is_up(n_ptr->links[i]));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100670 strcpy(link_info.str, n_ptr->links[i]->name);
Per Liden4323add2006-01-18 00:38:21 +0100671 tipc_cfg_append_tlv(buf, TIPC_TLV_LINK_INFO,
672 &link_info, sizeof(link_info));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100673 }
674 }
675
676 return buf;
677}