blob: 0d5db06e203f59a4da46b8a8182e86c7f5e462a7 [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
237 assert(bearer_id < MAX_BEARERS);
238 if (n_ptr->link_cnt >= 2) {
239 char addr_string[16];
240
241 err("Attempt to create third link to %s\n",
242 addr_string_fill(addr_string, n_ptr->addr));
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800243 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100244 }
245
246 if (!n_ptr->links[bearer_id]) {
247 n_ptr->links[bearer_id] = l_ptr;
Per Liden4323add2006-01-18 00:38:21 +0100248 tipc_net.zones[tipc_zone(l_ptr->addr)]->links++;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100249 n_ptr->link_cnt++;
250 return n_ptr;
251 }
252 err("Attempt to establish second link on <%s> to <%s> \n",
253 l_ptr->b_ptr->publ.name,
254 addr_string_fill(addr_string, l_ptr->addr));
255 }
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800256 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100257}
258
Per Liden4323add2006-01-18 00:38:21 +0100259void tipc_node_detach_link(struct node *n_ptr, struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100260{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800261 n_ptr->links[l_ptr->b_ptr->identity] = NULL;
Per Liden4323add2006-01-18 00:38:21 +0100262 tipc_net.zones[tipc_zone(l_ptr->addr)]->links--;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100263 n_ptr->link_cnt--;
264}
265
266/*
267 * Routing table management - five cases to handle:
268 *
269 * 1: A link towards a zone/cluster external node comes up.
270 * => Send a multicast message updating routing tables of all
271 * system nodes within own cluster that the new destination
272 * can be reached via this node.
273 * (node.establishedContact()=>cluster.multicastNewRoute())
274 *
275 * 2: A link towards a slave node comes up.
276 * => Send a multicast message updating routing tables of all
277 * system nodes within own cluster that the new destination
278 * can be reached via this node.
279 * (node.establishedContact()=>cluster.multicastNewRoute())
280 * => Send a message to the slave node about existence
281 * of all system nodes within cluster:
282 * (node.establishedContact()=>cluster.sendLocalRoutes())
283 *
284 * 3: A new cluster local system node becomes available.
285 * => Send message(s) to this particular node containing
286 * information about all cluster external and slave
287 * nodes which can be reached via this node.
288 * (node.establishedContact()==>network.sendExternalRoutes())
289 * (node.establishedContact()==>network.sendSlaveRoutes())
290 * => Send messages to all directly connected slave nodes
291 * containing information about the existence of the new node
292 * (node.establishedContact()=>cluster.multicastNewRoute())
293 *
294 * 4: The link towards a zone/cluster external node or slave
295 * node goes down.
296 * => Send a multcast message updating routing tables of all
297 * nodes within cluster that the new destination can not any
298 * longer be reached via this node.
299 * (node.lostAllLinks()=>cluster.bcastLostRoute())
300 *
301 * 5: A cluster local system node becomes unavailable.
302 * => Remove all references to this node from the local
303 * routing tables. Note: This is a completely node
304 * local operation.
305 * (node.lostAllLinks()=>network.removeAsRouter())
306 * => Send messages to all directly connected slave nodes
307 * containing information about loss of the node
308 * (node.establishedContact()=>cluster.multicastLostRoute())
309 *
310 */
311
312static void node_established_contact(struct node *n_ptr)
313{
314 struct cluster *c_ptr;
315
316 dbg("node_established_contact:-> %x\n", n_ptr->addr);
Per Liden4323add2006-01-18 00:38:21 +0100317 if (!tipc_node_has_active_routes(n_ptr)) {
318 tipc_k_signal((Handler)tipc_named_node_up, n_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100319 }
320
321 /* Syncronize broadcast acks */
Per Liden4323add2006-01-18 00:38:21 +0100322 n_ptr->bclink.acked = tipc_bclink_get_last_sent();
Per Lidenb97bf3f2006-01-02 19:04:38 +0100323
324 if (is_slave(tipc_own_addr))
325 return;
326 if (!in_own_cluster(n_ptr->addr)) {
327 /* Usage case 1 (see above) */
Per Liden4323add2006-01-18 00:38:21 +0100328 c_ptr = tipc_cltr_find(tipc_own_addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100329 if (!c_ptr)
Per Liden4323add2006-01-18 00:38:21 +0100330 c_ptr = tipc_cltr_create(tipc_own_addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100331 if (c_ptr)
Per Liden4323add2006-01-18 00:38:21 +0100332 tipc_cltr_bcast_new_route(c_ptr, n_ptr->addr, 1,
333 tipc_max_nodes);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100334 return;
335 }
336
337 c_ptr = n_ptr->owner;
338 if (is_slave(n_ptr->addr)) {
339 /* Usage case 2 (see above) */
Per Liden4323add2006-01-18 00:38:21 +0100340 tipc_cltr_bcast_new_route(c_ptr, n_ptr->addr, 1, tipc_max_nodes);
341 tipc_cltr_send_local_routes(c_ptr, n_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100342 return;
343 }
344
345 if (n_ptr->bclink.supported) {
Per Liden4323add2006-01-18 00:38:21 +0100346 tipc_nmap_add(&tipc_cltr_bcast_nodes, n_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100347 if (n_ptr->addr < tipc_own_addr)
348 tipc_own_tag++;
349 }
350
351 /* Case 3 (see above) */
Per Liden4323add2006-01-18 00:38:21 +0100352 tipc_net_send_external_routes(n_ptr->addr);
353 tipc_cltr_send_slave_routes(c_ptr, n_ptr->addr);
354 tipc_cltr_bcast_new_route(c_ptr, n_ptr->addr, LOWEST_SLAVE,
355 tipc_highest_allowed_slave);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100356}
357
358static void node_lost_contact(struct node *n_ptr)
359{
360 struct cluster *c_ptr;
361 struct node_subscr *ns, *tns;
362 char addr_string[16];
363 u32 i;
364
365 /* Clean up broadcast reception remains */
366 n_ptr->bclink.gap_after = n_ptr->bclink.gap_to = 0;
367 while (n_ptr->bclink.deferred_head) {
368 struct sk_buff* buf = n_ptr->bclink.deferred_head;
369 n_ptr->bclink.deferred_head = buf->next;
370 buf_discard(buf);
371 }
372 if (n_ptr->bclink.defragm) {
373 buf_discard(n_ptr->bclink.defragm);
374 n_ptr->bclink.defragm = NULL;
375 }
376 if (in_own_cluster(n_ptr->addr) && n_ptr->bclink.supported) {
Per Liden4323add2006-01-18 00:38:21 +0100377 tipc_bclink_acknowledge(n_ptr, mod(n_ptr->bclink.acked + 10000));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100378 }
379
380 /* Update routing tables */
381 if (is_slave(tipc_own_addr)) {
Per Liden4323add2006-01-18 00:38:21 +0100382 tipc_net_remove_as_router(n_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100383 } else {
384 if (!in_own_cluster(n_ptr->addr)) {
385 /* Case 4 (see above) */
Per Liden4323add2006-01-18 00:38:21 +0100386 c_ptr = tipc_cltr_find(tipc_own_addr);
387 tipc_cltr_bcast_lost_route(c_ptr, n_ptr->addr, 1,
388 tipc_max_nodes);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100389 } else {
390 /* Case 5 (see above) */
Per Liden4323add2006-01-18 00:38:21 +0100391 c_ptr = tipc_cltr_find(n_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100392 if (is_slave(n_ptr->addr)) {
Per Liden4323add2006-01-18 00:38:21 +0100393 tipc_cltr_bcast_lost_route(c_ptr, n_ptr->addr, 1,
394 tipc_max_nodes);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100395 } else {
396 if (n_ptr->bclink.supported) {
Per Liden4323add2006-01-18 00:38:21 +0100397 tipc_nmap_remove(&tipc_cltr_bcast_nodes,
398 n_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100399 if (n_ptr->addr < tipc_own_addr)
400 tipc_own_tag--;
401 }
Per Liden4323add2006-01-18 00:38:21 +0100402 tipc_net_remove_as_router(n_ptr->addr);
403 tipc_cltr_bcast_lost_route(c_ptr, n_ptr->addr,
404 LOWEST_SLAVE,
405 tipc_highest_allowed_slave);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100406 }
407 }
408 }
Per Liden4323add2006-01-18 00:38:21 +0100409 if (tipc_node_has_active_routes(n_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100410 return;
411
412 info("Lost contact with %s\n",
413 addr_string_fill(addr_string, n_ptr->addr));
414
415 /* Abort link changeover */
416 for (i = 0; i < MAX_BEARERS; i++) {
417 struct link *l_ptr = n_ptr->links[i];
418 if (!l_ptr)
419 continue;
420 l_ptr->reset_checkpoint = l_ptr->next_in_no;
421 l_ptr->exp_msg_count = 0;
Per Liden4323add2006-01-18 00:38:21 +0100422 tipc_link_reset_fragments(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100423 }
424
425 /* Notify subscribers */
426 list_for_each_entry_safe(ns, tns, &n_ptr->nsub, nodesub_list) {
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800427 ns->node = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100428 list_del_init(&ns->nodesub_list);
Per Liden4323add2006-01-18 00:38:21 +0100429 tipc_k_signal((Handler)ns->handle_node_down,
430 (unsigned long)ns->usr_handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100431 }
432}
433
434/**
Per Liden4323add2006-01-18 00:38:21 +0100435 * tipc_node_select_next_hop - find the next-hop node for a message
Per Lidenb97bf3f2006-01-02 19:04:38 +0100436 *
437 * Called by when cluster local lookup has failed.
438 */
439
Per Liden4323add2006-01-18 00:38:21 +0100440struct node *tipc_node_select_next_hop(u32 addr, u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100441{
442 struct node *n_ptr;
443 u32 router_addr;
444
Per Liden4323add2006-01-18 00:38:21 +0100445 if (!tipc_addr_domain_valid(addr))
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800446 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100447
448 /* Look for direct link to destination processsor */
Per Liden4323add2006-01-18 00:38:21 +0100449 n_ptr = tipc_node_find(addr);
450 if (n_ptr && tipc_node_has_active_links(n_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100451 return n_ptr;
452
453 /* Cluster local system nodes *must* have direct links */
454 if (!is_slave(addr) && in_own_cluster(addr))
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800455 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100456
457 /* Look for cluster local router with direct link to node */
Per Liden4323add2006-01-18 00:38:21 +0100458 router_addr = tipc_node_select_router(n_ptr, selector);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100459 if (router_addr)
Per Liden4323add2006-01-18 00:38:21 +0100460 return tipc_node_select(router_addr, selector);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100461
462 /* Slave nodes can only be accessed within own cluster via a
463 known router with direct link -- if no router was found,give up */
464 if (is_slave(addr))
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800465 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100466
467 /* Inter zone/cluster -- find any direct link to remote cluster */
468 addr = tipc_addr(tipc_zone(addr), tipc_cluster(addr), 0);
Per Liden4323add2006-01-18 00:38:21 +0100469 n_ptr = tipc_net_select_remote_node(addr, selector);
470 if (n_ptr && tipc_node_has_active_links(n_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100471 return n_ptr;
472
473 /* Last resort -- look for any router to anywhere in remote zone */
Per Liden4323add2006-01-18 00:38:21 +0100474 router_addr = tipc_net_select_router(addr, selector);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100475 if (router_addr)
Per Liden4323add2006-01-18 00:38:21 +0100476 return tipc_node_select(router_addr, selector);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100477
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800478 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100479}
480
481/**
Per Liden4323add2006-01-18 00:38:21 +0100482 * tipc_node_select_router - select router to reach specified node
Per Lidenb97bf3f2006-01-02 19:04:38 +0100483 *
484 * Uses a deterministic and fair algorithm for selecting router node.
485 */
486
Per Liden4323add2006-01-18 00:38:21 +0100487u32 tipc_node_select_router(struct node *n_ptr, u32 ref)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100488{
489 u32 ulim;
490 u32 mask;
491 u32 start;
492 u32 r;
493
494 if (!n_ptr)
495 return 0;
496
497 if (n_ptr->last_router < 0)
498 return 0;
499 ulim = ((n_ptr->last_router + 1) * 32) - 1;
500
501 /* Start entry must be random */
502 mask = tipc_max_nodes;
503 while (mask > ulim)
504 mask >>= 1;
505 start = ref & mask;
506 r = start;
507
508 /* Lookup upwards with wrap-around */
509 do {
510 if (((n_ptr->routers[r / 32]) >> (r % 32)) & 1)
511 break;
512 } while (++r <= ulim);
513 if (r > ulim) {
514 r = 1;
515 do {
516 if (((n_ptr->routers[r / 32]) >> (r % 32)) & 1)
517 break;
518 } while (++r < start);
519 assert(r != start);
520 }
521 assert(r && (r <= ulim));
522 return tipc_addr(own_zone(), own_cluster(), r);
523}
524
Per Liden4323add2006-01-18 00:38:21 +0100525void tipc_node_add_router(struct node *n_ptr, u32 router)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100526{
527 u32 r_num = tipc_node(router);
528
529 n_ptr->routers[r_num / 32] =
530 ((1 << (r_num % 32)) | n_ptr->routers[r_num / 32]);
531 n_ptr->last_router = tipc_max_nodes / 32;
532 while ((--n_ptr->last_router >= 0) &&
533 !n_ptr->routers[n_ptr->last_router]);
534}
535
Per Liden4323add2006-01-18 00:38:21 +0100536void tipc_node_remove_router(struct node *n_ptr, u32 router)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100537{
538 u32 r_num = tipc_node(router);
539
540 if (n_ptr->last_router < 0)
541 return; /* No routes */
542
543 n_ptr->routers[r_num / 32] =
544 ((~(1 << (r_num % 32))) & (n_ptr->routers[r_num / 32]));
545 n_ptr->last_router = tipc_max_nodes / 32;
546 while ((--n_ptr->last_router >= 0) &&
547 !n_ptr->routers[n_ptr->last_router]);
548
Per Liden4323add2006-01-18 00:38:21 +0100549 if (!tipc_node_is_up(n_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100550 node_lost_contact(n_ptr);
551}
552
553#if 0
554void node_print(struct print_buf *buf, struct node *n_ptr, char *str)
555{
556 u32 i;
557
558 tipc_printf(buf, "\n\n%s", str);
559 for (i = 0; i < MAX_BEARERS; i++) {
560 if (!n_ptr->links[i])
561 continue;
562 tipc_printf(buf, "Links[%u]: %x, ", i, n_ptr->links[i]);
563 }
564 tipc_printf(buf, "Active links: [%x,%x]\n",
565 n_ptr->active_links[0], n_ptr->active_links[1]);
566}
567#endif
568
569u32 tipc_available_nodes(const u32 domain)
570{
571 struct node *n_ptr;
572 u32 cnt = 0;
573
Per Liden4323add2006-01-18 00:38:21 +0100574 for (n_ptr = tipc_nodes; n_ptr; n_ptr = n_ptr->next) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100575 if (!in_scope(domain, n_ptr->addr))
576 continue;
Per Liden4323add2006-01-18 00:38:21 +0100577 if (tipc_node_is_up(n_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100578 cnt++;
579 }
580 return cnt;
581}
582
Per Liden4323add2006-01-18 00:38:21 +0100583struct sk_buff *tipc_node_get_nodes(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100584{
585 u32 domain;
586 struct sk_buff *buf;
587 struct node *n_ptr;
588 struct tipc_node_info node_info;
589
590 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NET_ADDR))
Per Liden4323add2006-01-18 00:38:21 +0100591 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100592
593 domain = *(u32 *)TLV_DATA(req_tlv_area);
594 domain = ntohl(domain);
Per Liden4323add2006-01-18 00:38:21 +0100595 if (!tipc_addr_domain_valid(domain))
596 return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
597 " (network address)");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100598
Per Liden4323add2006-01-18 00:38:21 +0100599 if (!tipc_nodes)
600 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +0100601
602 /* For now, get space for all other nodes
603 (will need to modify this when slave nodes are supported */
604
Per Liden4323add2006-01-18 00:38:21 +0100605 buf = tipc_cfg_reply_alloc(TLV_SPACE(sizeof(node_info)) *
606 (tipc_max_nodes - 1));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100607 if (!buf)
608 return NULL;
609
610 /* Add TLVs for all nodes in scope */
611
Per Liden4323add2006-01-18 00:38:21 +0100612 for (n_ptr = tipc_nodes; n_ptr; n_ptr = n_ptr->next) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100613 if (!in_scope(domain, n_ptr->addr))
614 continue;
615 node_info.addr = htonl(n_ptr->addr);
Per Liden4323add2006-01-18 00:38:21 +0100616 node_info.up = htonl(tipc_node_is_up(n_ptr));
617 tipc_cfg_append_tlv(buf, TIPC_TLV_NODE_INFO,
618 &node_info, sizeof(node_info));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100619 }
620
621 return buf;
622}
623
Per Liden4323add2006-01-18 00:38:21 +0100624struct sk_buff *tipc_node_get_links(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100625{
626 u32 domain;
627 struct sk_buff *buf;
628 struct node *n_ptr;
629 struct tipc_link_info link_info;
630
631 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NET_ADDR))
Per Liden4323add2006-01-18 00:38:21 +0100632 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100633
634 domain = *(u32 *)TLV_DATA(req_tlv_area);
635 domain = ntohl(domain);
Per Liden4323add2006-01-18 00:38:21 +0100636 if (!tipc_addr_domain_valid(domain))
637 return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
638 " (network address)");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100639
Per Liden4323add2006-01-18 00:38:21 +0100640 if (!tipc_nodes)
641 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +0100642
643 /* For now, get space for 2 links to all other nodes + bcast link
644 (will need to modify this when slave nodes are supported */
645
Per Liden4323add2006-01-18 00:38:21 +0100646 buf = tipc_cfg_reply_alloc(TLV_SPACE(sizeof(link_info)) *
647 (2 * (tipc_max_nodes - 1) + 1));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100648 if (!buf)
649 return NULL;
650
651 /* Add TLV for broadcast link */
652
653 link_info.dest = tipc_own_addr & 0xfffff00;
654 link_info.dest = htonl(link_info.dest);
655 link_info.up = htonl(1);
Per Liden4323add2006-01-18 00:38:21 +0100656 sprintf(link_info.str, tipc_bclink_name);
657 tipc_cfg_append_tlv(buf, TIPC_TLV_LINK_INFO, &link_info, sizeof(link_info));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100658
659 /* Add TLVs for any other links in scope */
660
Per Liden4323add2006-01-18 00:38:21 +0100661 for (n_ptr = tipc_nodes; n_ptr; n_ptr = n_ptr->next) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100662 u32 i;
663
664 if (!in_scope(domain, n_ptr->addr))
665 continue;
666 for (i = 0; i < MAX_BEARERS; i++) {
667 if (!n_ptr->links[i])
668 continue;
669 link_info.dest = htonl(n_ptr->addr);
Per Liden4323add2006-01-18 00:38:21 +0100670 link_info.up = htonl(tipc_link_is_up(n_ptr->links[i]));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100671 strcpy(link_info.str, n_ptr->links[i]->name);
Per Liden4323add2006-01-18 00:38:21 +0100672 tipc_cfg_append_tlv(buf, TIPC_TLV_LINK_INFO,
673 &link_info, sizeof(link_info));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100674 }
675 }
676
677 return buf;
678}