blob: 598f4d3a0098e4f9b1ba5ae5f166f5166db1e069 [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
2 * net/tipc/node.c: TIPC node management routines
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Per Liden593a5f22006-01-11 19:14:19 +01004 * Copyright (c) 2000-2006, Ericsson AB
Allan Stephensea138472006-06-29 12:33:20 -07005 * Copyright (c) 2005-2006, 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;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090061 struct node **curr_node;
Per Lidenb97bf3f2006-01-02 19:04:38 +010062
Arnaldo Carvalho de Melo2710b572006-11-21 01:22:12 -020063 n_ptr = kzalloc(sizeof(*n_ptr),GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -070064 if (!n_ptr) {
65 warn("Node creation failed, no memory\n");
66 return NULL;
67 }
Per Lidenb97bf3f2006-01-02 19:04:38 +010068
Allan Stephensa10bd922006-06-25 23:52:17 -070069 c_ptr = tipc_cltr_find(addr);
70 if (!c_ptr) {
71 c_ptr = tipc_cltr_create(addr);
72 }
73 if (!c_ptr) {
74 kfree(n_ptr);
75 return NULL;
76 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090077
Allan Stephensa10bd922006-06-25 23:52:17 -070078 n_ptr->addr = addr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090079 spin_lock_init(&n_ptr->lock);
Allan Stephensa10bd922006-06-25 23:52:17 -070080 INIT_LIST_HEAD(&n_ptr->nsub);
81 n_ptr->owner = c_ptr;
82 tipc_cltr_attach_node(c_ptr, n_ptr);
83 n_ptr->last_router = -1;
84
85 /* Insert node into ordered list */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090086 for (curr_node = &tipc_nodes; *curr_node;
Allan Stephensa10bd922006-06-25 23:52:17 -070087 curr_node = &(*curr_node)->next) {
88 if (addr < (*curr_node)->addr) {
89 n_ptr->next = *curr_node;
90 break;
91 }
92 }
93 (*curr_node) = n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +010094 return n_ptr;
95}
96
Per Liden4323add2006-01-18 00:38:21 +010097void tipc_node_delete(struct node *n_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +010098{
99 if (!n_ptr)
100 return;
101
102#if 0
Per Liden4323add2006-01-18 00:38:21 +0100103 /* Not needed because links are already deleted via tipc_bearer_stop() */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100104
105 u32 l_num;
106
107 for (l_num = 0; l_num < MAX_BEARERS; l_num++) {
108 link_delete(n_ptr->links[l_num]);
109 }
110#endif
111
112 dbg("node %x deleted\n", n_ptr->addr);
113 kfree(n_ptr);
114}
115
116
117/**
Per Liden4323add2006-01-18 00:38:21 +0100118 * tipc_node_link_up - handle addition of link
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900119 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100120 * Link becomes active (alone or shared) or standby, depending on its priority.
121 */
122
Per Liden4323add2006-01-18 00:38:21 +0100123void tipc_node_link_up(struct node *n_ptr, struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100124{
125 struct link **active = &n_ptr->active_links[0];
126
Allan Stephens5392d642006-06-25 23:52:50 -0700127 n_ptr->working_links++;
128
Per Lidenb97bf3f2006-01-02 19:04:38 +0100129 info("Established link <%s> on network plane %c\n",
130 l_ptr->name, l_ptr->b_ptr->net_plane);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900131
Per Lidenb97bf3f2006-01-02 19:04:38 +0100132 if (!active[0]) {
133 dbg(" link %x into %x/%x\n", l_ptr, &active[0], &active[1]);
134 active[0] = active[1] = l_ptr;
135 node_established_contact(n_ptr);
136 return;
137 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900138 if (l_ptr->priority < active[0]->priority) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700139 info("New link <%s> becomes standby\n", l_ptr->name);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100140 return;
141 }
Per Liden4323add2006-01-18 00:38:21 +0100142 tipc_link_send_duplicate(active[0], l_ptr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900143 if (l_ptr->priority == active[0]->priority) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100144 active[0] = l_ptr;
145 return;
146 }
Allan Stephensa10bd922006-06-25 23:52:17 -0700147 info("Old link <%s> becomes standby\n", active[0]->name);
148 if (active[1] != active[0])
149 info("Old link <%s> becomes standby\n", active[1]->name);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100150 active[0] = active[1] = l_ptr;
151}
152
153/**
154 * node_select_active_links - select active link
155 */
156
157static void node_select_active_links(struct node *n_ptr)
158{
159 struct link **active = &n_ptr->active_links[0];
160 u32 i;
161 u32 highest_prio = 0;
162
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900163 active[0] = active[1] = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100164
165 for (i = 0; i < MAX_BEARERS; i++) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900166 struct link *l_ptr = n_ptr->links[i];
Per Lidenb97bf3f2006-01-02 19:04:38 +0100167
Per Liden4323add2006-01-18 00:38:21 +0100168 if (!l_ptr || !tipc_link_is_up(l_ptr) ||
Per Lidenb97bf3f2006-01-02 19:04:38 +0100169 (l_ptr->priority < highest_prio))
170 continue;
171
172 if (l_ptr->priority > highest_prio) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900173 highest_prio = l_ptr->priority;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100174 active[0] = active[1] = l_ptr;
175 } else {
176 active[1] = l_ptr;
177 }
178 }
179}
180
181/**
Per Liden4323add2006-01-18 00:38:21 +0100182 * tipc_node_link_down - handle loss of link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100183 */
184
Per Liden4323add2006-01-18 00:38:21 +0100185void tipc_node_link_down(struct node *n_ptr, struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100186{
187 struct link **active;
188
Allan Stephens5392d642006-06-25 23:52:50 -0700189 n_ptr->working_links--;
190
Per Liden4323add2006-01-18 00:38:21 +0100191 if (!tipc_link_is_active(l_ptr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100192 info("Lost standby link <%s> on network plane %c\n",
193 l_ptr->name, l_ptr->b_ptr->net_plane);
194 return;
195 }
196 info("Lost link <%s> on network plane %c\n",
197 l_ptr->name, l_ptr->b_ptr->net_plane);
198
199 active = &n_ptr->active_links[0];
200 if (active[0] == l_ptr)
201 active[0] = active[1];
202 if (active[1] == l_ptr)
203 active[1] = active[0];
204 if (active[0] == l_ptr)
205 node_select_active_links(n_ptr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900206 if (tipc_node_is_up(n_ptr))
Per Liden4323add2006-01-18 00:38:21 +0100207 tipc_link_changeover(l_ptr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900208 else
Per Lidenb97bf3f2006-01-02 19:04:38 +0100209 node_lost_contact(n_ptr);
210}
211
Per Liden4323add2006-01-18 00:38:21 +0100212int tipc_node_has_active_links(struct node *n_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100213{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900214 return (n_ptr &&
Per Lidenb97bf3f2006-01-02 19:04:38 +0100215 ((n_ptr->active_links[0]) || (n_ptr->active_links[1])));
216}
217
Per Liden4323add2006-01-18 00:38:21 +0100218int tipc_node_has_redundant_links(struct node *n_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100219{
Allan Stephens5392d642006-06-25 23:52:50 -0700220 return (n_ptr->working_links > 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100221}
222
Adrian Bunk988f0882006-03-20 22:37:52 -0800223static int tipc_node_has_active_routes(struct node *n_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100224{
225 return (n_ptr && (n_ptr->last_router >= 0));
226}
227
Per Liden4323add2006-01-18 00:38:21 +0100228int tipc_node_is_up(struct node *n_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100229{
Per Liden4323add2006-01-18 00:38:21 +0100230 return (tipc_node_has_active_links(n_ptr) || tipc_node_has_active_routes(n_ptr));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100231}
232
Per Liden4323add2006-01-18 00:38:21 +0100233struct node *tipc_node_attach_link(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100234{
Per Liden4323add2006-01-18 00:38:21 +0100235 struct node *n_ptr = tipc_node_find(l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100236
237 if (!n_ptr)
Per Liden4323add2006-01-18 00:38:21 +0100238 n_ptr = tipc_node_create(l_ptr->addr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900239 if (n_ptr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100240 u32 bearer_id = l_ptr->b_ptr->identity;
241 char addr_string[16];
242
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900243 if (n_ptr->link_cnt >= 2) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900244 err("Attempt to create third link to %s\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100245 addr_string_fill(addr_string, n_ptr->addr));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900246 return NULL;
247 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100248
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900249 if (!n_ptr->links[bearer_id]) {
250 n_ptr->links[bearer_id] = l_ptr;
251 tipc_net.zones[tipc_zone(l_ptr->addr)]->links++;
252 n_ptr->link_cnt++;
253 return n_ptr;
254 }
255 err("Attempt to establish second link on <%s> to %s \n",
256 l_ptr->b_ptr->publ.name,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100257 addr_string_fill(addr_string, l_ptr->addr));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900258 }
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800259 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100260}
261
Per Liden4323add2006-01-18 00:38:21 +0100262void tipc_node_detach_link(struct node *n_ptr, struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100263{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800264 n_ptr->links[l_ptr->b_ptr->identity] = NULL;
Per Liden4323add2006-01-18 00:38:21 +0100265 tipc_net.zones[tipc_zone(l_ptr->addr)]->links--;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100266 n_ptr->link_cnt--;
267}
268
269/*
270 * Routing table management - five cases to handle:
271 *
272 * 1: A link towards a zone/cluster external node comes up.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900273 * => Send a multicast message updating routing tables of all
274 * system nodes within own cluster that the new destination
275 * can be reached via this node.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100276 * (node.establishedContact()=>cluster.multicastNewRoute())
277 *
278 * 2: A link towards a slave node comes up.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900279 * => Send a multicast message updating routing tables of all
280 * system nodes within own cluster that the new destination
281 * can be reached via this node.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100282 * (node.establishedContact()=>cluster.multicastNewRoute())
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900283 * => Send a message to the slave node about existence
Per Lidenb97bf3f2006-01-02 19:04:38 +0100284 * of all system nodes within cluster:
285 * (node.establishedContact()=>cluster.sendLocalRoutes())
286 *
287 * 3: A new cluster local system node becomes available.
288 * => Send message(s) to this particular node containing
289 * information about all cluster external and slave
290 * nodes which can be reached via this node.
291 * (node.establishedContact()==>network.sendExternalRoutes())
292 * (node.establishedContact()==>network.sendSlaveRoutes())
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900293 * => Send messages to all directly connected slave nodes
Per Lidenb97bf3f2006-01-02 19:04:38 +0100294 * containing information about the existence of the new node
295 * (node.establishedContact()=>cluster.multicastNewRoute())
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900296 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100297 * 4: The link towards a zone/cluster external node or slave
298 * node goes down.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900299 * => Send a multcast message updating routing tables of all
Per Lidenb97bf3f2006-01-02 19:04:38 +0100300 * nodes within cluster that the new destination can not any
301 * longer be reached via this node.
302 * (node.lostAllLinks()=>cluster.bcastLostRoute())
303 *
304 * 5: A cluster local system node becomes unavailable.
305 * => Remove all references to this node from the local
306 * routing tables. Note: This is a completely node
307 * local operation.
308 * (node.lostAllLinks()=>network.removeAsRouter())
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900309 * => Send messages to all directly connected slave nodes
Per Lidenb97bf3f2006-01-02 19:04:38 +0100310 * containing information about loss of the node
311 * (node.establishedContact()=>cluster.multicastLostRoute())
312 *
313 */
314
315static void node_established_contact(struct node *n_ptr)
316{
317 struct cluster *c_ptr;
318
319 dbg("node_established_contact:-> %x\n", n_ptr->addr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900320 if (!tipc_node_has_active_routes(n_ptr) && in_own_cluster(n_ptr->addr)) {
Per Liden4323add2006-01-18 00:38:21 +0100321 tipc_k_signal((Handler)tipc_named_node_up, n_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100322 }
323
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900324 /* Syncronize broadcast acks */
325 n_ptr->bclink.acked = tipc_bclink_get_last_sent();
Per Lidenb97bf3f2006-01-02 19:04:38 +0100326
327 if (is_slave(tipc_own_addr))
328 return;
329 if (!in_own_cluster(n_ptr->addr)) {
330 /* Usage case 1 (see above) */
Per Liden4323add2006-01-18 00:38:21 +0100331 c_ptr = tipc_cltr_find(tipc_own_addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100332 if (!c_ptr)
Per Liden4323add2006-01-18 00:38:21 +0100333 c_ptr = tipc_cltr_create(tipc_own_addr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900334 if (c_ptr)
335 tipc_cltr_bcast_new_route(c_ptr, n_ptr->addr, 1,
Per Liden4323add2006-01-18 00:38:21 +0100336 tipc_max_nodes);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100337 return;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900338 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100339
340 c_ptr = n_ptr->owner;
341 if (is_slave(n_ptr->addr)) {
342 /* Usage case 2 (see above) */
Per Liden4323add2006-01-18 00:38:21 +0100343 tipc_cltr_bcast_new_route(c_ptr, n_ptr->addr, 1, tipc_max_nodes);
344 tipc_cltr_send_local_routes(c_ptr, n_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100345 return;
346 }
347
348 if (n_ptr->bclink.supported) {
Per Liden4323add2006-01-18 00:38:21 +0100349 tipc_nmap_add(&tipc_cltr_bcast_nodes, n_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100350 if (n_ptr->addr < tipc_own_addr)
351 tipc_own_tag++;
352 }
353
354 /* Case 3 (see above) */
Per Liden4323add2006-01-18 00:38:21 +0100355 tipc_net_send_external_routes(n_ptr->addr);
356 tipc_cltr_send_slave_routes(c_ptr, n_ptr->addr);
357 tipc_cltr_bcast_new_route(c_ptr, n_ptr->addr, LOWEST_SLAVE,
358 tipc_highest_allowed_slave);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100359}
360
361static void node_lost_contact(struct node *n_ptr)
362{
363 struct cluster *c_ptr;
364 struct node_subscr *ns, *tns;
365 char addr_string[16];
366 u32 i;
367
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900368 /* Clean up broadcast reception remains */
369 n_ptr->bclink.gap_after = n_ptr->bclink.gap_to = 0;
370 while (n_ptr->bclink.deferred_head) {
371 struct sk_buff* buf = n_ptr->bclink.deferred_head;
372 n_ptr->bclink.deferred_head = buf->next;
373 buf_discard(buf);
374 }
375 if (n_ptr->bclink.defragm) {
376 buf_discard(n_ptr->bclink.defragm);
377 n_ptr->bclink.defragm = NULL;
378 }
379 if (in_own_cluster(n_ptr->addr) && n_ptr->bclink.supported) {
380 tipc_bclink_acknowledge(n_ptr, mod(n_ptr->bclink.acked + 10000));
381 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100382
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900383 /* Update routing tables */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100384 if (is_slave(tipc_own_addr)) {
Per Liden4323add2006-01-18 00:38:21 +0100385 tipc_net_remove_as_router(n_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100386 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900387 if (!in_own_cluster(n_ptr->addr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100388 /* Case 4 (see above) */
Per Liden4323add2006-01-18 00:38:21 +0100389 c_ptr = tipc_cltr_find(tipc_own_addr);
390 tipc_cltr_bcast_lost_route(c_ptr, n_ptr->addr, 1,
391 tipc_max_nodes);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100392 } else {
393 /* Case 5 (see above) */
Per Liden4323add2006-01-18 00:38:21 +0100394 c_ptr = tipc_cltr_find(n_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100395 if (is_slave(n_ptr->addr)) {
Per Liden4323add2006-01-18 00:38:21 +0100396 tipc_cltr_bcast_lost_route(c_ptr, n_ptr->addr, 1,
397 tipc_max_nodes);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100398 } else {
399 if (n_ptr->bclink.supported) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900400 tipc_nmap_remove(&tipc_cltr_bcast_nodes,
Per Liden4323add2006-01-18 00:38:21 +0100401 n_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100402 if (n_ptr->addr < tipc_own_addr)
403 tipc_own_tag--;
404 }
Per Liden4323add2006-01-18 00:38:21 +0100405 tipc_net_remove_as_router(n_ptr->addr);
406 tipc_cltr_bcast_lost_route(c_ptr, n_ptr->addr,
407 LOWEST_SLAVE,
408 tipc_highest_allowed_slave);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100409 }
410 }
411 }
Per Liden4323add2006-01-18 00:38:21 +0100412 if (tipc_node_has_active_routes(n_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100413 return;
414
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900415 info("Lost contact with %s\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100416 addr_string_fill(addr_string, n_ptr->addr));
417
418 /* Abort link changeover */
419 for (i = 0; i < MAX_BEARERS; i++) {
420 struct link *l_ptr = n_ptr->links[i];
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900421 if (!l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100422 continue;
423 l_ptr->reset_checkpoint = l_ptr->next_in_no;
424 l_ptr->exp_msg_count = 0;
Per Liden4323add2006-01-18 00:38:21 +0100425 tipc_link_reset_fragments(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100426 }
427
428 /* Notify subscribers */
429 list_for_each_entry_safe(ns, tns, &n_ptr->nsub, nodesub_list) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900430 ns->node = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100431 list_del_init(&ns->nodesub_list);
Per Liden4323add2006-01-18 00:38:21 +0100432 tipc_k_signal((Handler)ns->handle_node_down,
433 (unsigned long)ns->usr_handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100434 }
435}
436
437/**
Per Liden4323add2006-01-18 00:38:21 +0100438 * tipc_node_select_next_hop - find the next-hop node for a message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900439 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100440 * Called by when cluster local lookup has failed.
441 */
442
Per Liden4323add2006-01-18 00:38:21 +0100443struct node *tipc_node_select_next_hop(u32 addr, u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100444{
445 struct node *n_ptr;
446 u32 router_addr;
447
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900448 if (!tipc_addr_domain_valid(addr))
449 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100450
451 /* Look for direct link to destination processsor */
Per Liden4323add2006-01-18 00:38:21 +0100452 n_ptr = tipc_node_find(addr);
453 if (n_ptr && tipc_node_has_active_links(n_ptr))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900454 return n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100455
456 /* Cluster local system nodes *must* have direct links */
457 if (!is_slave(addr) && in_own_cluster(addr))
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800458 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100459
460 /* Look for cluster local router with direct link to node */
Per Liden4323add2006-01-18 00:38:21 +0100461 router_addr = tipc_node_select_router(n_ptr, selector);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900462 if (router_addr)
463 return tipc_node_select(router_addr, selector);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100464
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900465 /* Slave nodes can only be accessed within own cluster via a
Per Lidenb97bf3f2006-01-02 19:04:38 +0100466 known router with direct link -- if no router was found,give up */
467 if (is_slave(addr))
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800468 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100469
470 /* Inter zone/cluster -- find any direct link to remote cluster */
471 addr = tipc_addr(tipc_zone(addr), tipc_cluster(addr), 0);
Per Liden4323add2006-01-18 00:38:21 +0100472 n_ptr = tipc_net_select_remote_node(addr, selector);
473 if (n_ptr && tipc_node_has_active_links(n_ptr))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900474 return n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100475
476 /* Last resort -- look for any router to anywhere in remote zone */
Per Liden4323add2006-01-18 00:38:21 +0100477 router_addr = tipc_net_select_router(addr, selector);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900478 if (router_addr)
479 return tipc_node_select(router_addr, selector);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100480
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900481 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100482}
483
484/**
Per Liden4323add2006-01-18 00:38:21 +0100485 * tipc_node_select_router - select router to reach specified node
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900486 *
487 * Uses a deterministic and fair algorithm for selecting router node.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100488 */
489
Per Liden4323add2006-01-18 00:38:21 +0100490u32 tipc_node_select_router(struct node *n_ptr, u32 ref)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100491{
492 u32 ulim;
493 u32 mask;
494 u32 start;
495 u32 r;
496
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900497 if (!n_ptr)
498 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100499
500 if (n_ptr->last_router < 0)
501 return 0;
502 ulim = ((n_ptr->last_router + 1) * 32) - 1;
503
504 /* Start entry must be random */
505 mask = tipc_max_nodes;
506 while (mask > ulim)
507 mask >>= 1;
508 start = ref & mask;
509 r = start;
510
511 /* Lookup upwards with wrap-around */
512 do {
513 if (((n_ptr->routers[r / 32]) >> (r % 32)) & 1)
514 break;
515 } while (++r <= ulim);
516 if (r > ulim) {
517 r = 1;
518 do {
519 if (((n_ptr->routers[r / 32]) >> (r % 32)) & 1)
520 break;
521 } while (++r < start);
522 assert(r != start);
523 }
524 assert(r && (r <= ulim));
525 return tipc_addr(own_zone(), own_cluster(), r);
526}
527
Per Liden4323add2006-01-18 00:38:21 +0100528void tipc_node_add_router(struct node *n_ptr, u32 router)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100529{
530 u32 r_num = tipc_node(router);
531
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900532 n_ptr->routers[r_num / 32] =
Per Lidenb97bf3f2006-01-02 19:04:38 +0100533 ((1 << (r_num % 32)) | n_ptr->routers[r_num / 32]);
534 n_ptr->last_router = tipc_max_nodes / 32;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900535 while ((--n_ptr->last_router >= 0) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +0100536 !n_ptr->routers[n_ptr->last_router]);
537}
538
Per Liden4323add2006-01-18 00:38:21 +0100539void tipc_node_remove_router(struct node *n_ptr, u32 router)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100540{
541 u32 r_num = tipc_node(router);
542
543 if (n_ptr->last_router < 0)
544 return; /* No routes */
545
546 n_ptr->routers[r_num / 32] =
547 ((~(1 << (r_num % 32))) & (n_ptr->routers[r_num / 32]));
548 n_ptr->last_router = tipc_max_nodes / 32;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900549 while ((--n_ptr->last_router >= 0) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +0100550 !n_ptr->routers[n_ptr->last_router]);
551
Per Liden4323add2006-01-18 00:38:21 +0100552 if (!tipc_node_is_up(n_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100553 node_lost_contact(n_ptr);
554}
555
556#if 0
557void node_print(struct print_buf *buf, struct node *n_ptr, char *str)
558{
559 u32 i;
560
561 tipc_printf(buf, "\n\n%s", str);
562 for (i = 0; i < MAX_BEARERS; i++) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900563 if (!n_ptr->links[i])
Per Lidenb97bf3f2006-01-02 19:04:38 +0100564 continue;
565 tipc_printf(buf, "Links[%u]: %x, ", i, n_ptr->links[i]);
566 }
567 tipc_printf(buf, "Active links: [%x,%x]\n",
568 n_ptr->active_links[0], n_ptr->active_links[1]);
569}
570#endif
571
572u32 tipc_available_nodes(const u32 domain)
573{
574 struct node *n_ptr;
575 u32 cnt = 0;
576
Per Liden4323add2006-01-18 00:38:21 +0100577 for (n_ptr = tipc_nodes; n_ptr; n_ptr = n_ptr->next) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100578 if (!in_scope(domain, n_ptr->addr))
579 continue;
Per Liden4323add2006-01-18 00:38:21 +0100580 if (tipc_node_is_up(n_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100581 cnt++;
582 }
583 return cnt;
584}
585
Per Liden4323add2006-01-18 00:38:21 +0100586struct sk_buff *tipc_node_get_nodes(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100587{
588 u32 domain;
589 struct sk_buff *buf;
590 struct node *n_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900591 struct tipc_node_info node_info;
Allan Stephensea138472006-06-29 12:33:20 -0700592 u32 payload_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100593
594 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NET_ADDR))
Per Liden4323add2006-01-18 00:38:21 +0100595 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100596
Al Viro3e6c8cd2006-11-08 00:19:09 -0800597 domain = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
Per Liden4323add2006-01-18 00:38:21 +0100598 if (!tipc_addr_domain_valid(domain))
599 return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
600 " (network address)");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100601
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900602 if (!tipc_nodes)
603 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +0100604
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900605 /* For now, get space for all other nodes
Per Lidenb97bf3f2006-01-02 19:04:38 +0100606 (will need to modify this when slave nodes are supported */
607
Allan Stephensea138472006-06-29 12:33:20 -0700608 payload_size = TLV_SPACE(sizeof(node_info)) * (tipc_max_nodes - 1);
609 if (payload_size > 32768u)
610 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
611 " (too many nodes)");
612 buf = tipc_cfg_reply_alloc(payload_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100613 if (!buf)
614 return NULL;
615
616 /* Add TLVs for all nodes in scope */
617
Per Liden4323add2006-01-18 00:38:21 +0100618 for (n_ptr = tipc_nodes; n_ptr; n_ptr = n_ptr->next) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100619 if (!in_scope(domain, n_ptr->addr))
620 continue;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900621 node_info.addr = htonl(n_ptr->addr);
622 node_info.up = htonl(tipc_node_is_up(n_ptr));
623 tipc_cfg_append_tlv(buf, TIPC_TLV_NODE_INFO,
Per Liden4323add2006-01-18 00:38:21 +0100624 &node_info, sizeof(node_info));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100625 }
626
627 return buf;
628}
629
Per Liden4323add2006-01-18 00:38:21 +0100630struct sk_buff *tipc_node_get_links(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100631{
632 u32 domain;
633 struct sk_buff *buf;
634 struct node *n_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900635 struct tipc_link_info link_info;
Allan Stephensea138472006-06-29 12:33:20 -0700636 u32 payload_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100637
638 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NET_ADDR))
Per Liden4323add2006-01-18 00:38:21 +0100639 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100640
Al Viro3e6c8cd2006-11-08 00:19:09 -0800641 domain = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
Per Liden4323add2006-01-18 00:38:21 +0100642 if (!tipc_addr_domain_valid(domain))
643 return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
644 " (network address)");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100645
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900646 if (tipc_mode != TIPC_NET_MODE)
647 return tipc_cfg_reply_none();
648
Allan Stephensea138472006-06-29 12:33:20 -0700649 /* Get space for all unicast links + multicast link */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100650
Allan Stephensea138472006-06-29 12:33:20 -0700651 payload_size = TLV_SPACE(sizeof(link_info)) *
652 (tipc_net.zones[tipc_zone(tipc_own_addr)]->links + 1);
653 if (payload_size > 32768u)
654 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
655 " (too many links)");
656 buf = tipc_cfg_reply_alloc(payload_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100657 if (!buf)
658 return NULL;
659
660 /* Add TLV for broadcast link */
661
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900662 link_info.dest = htonl(tipc_own_addr & 0xfffff00);
663 link_info.up = htonl(1);
664 sprintf(link_info.str, tipc_bclink_name);
Per Liden4323add2006-01-18 00:38:21 +0100665 tipc_cfg_append_tlv(buf, TIPC_TLV_LINK_INFO, &link_info, sizeof(link_info));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100666
667 /* Add TLVs for any other links in scope */
668
Per Liden4323add2006-01-18 00:38:21 +0100669 for (n_ptr = tipc_nodes; n_ptr; n_ptr = n_ptr->next) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900670 u32 i;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100671
672 if (!in_scope(domain, n_ptr->addr))
673 continue;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900674 for (i = 0; i < MAX_BEARERS; i++) {
675 if (!n_ptr->links[i])
676 continue;
677 link_info.dest = htonl(n_ptr->addr);
678 link_info.up = htonl(tipc_link_is_up(n_ptr->links[i]));
679 strcpy(link_info.str, n_ptr->links[i]->name);
680 tipc_cfg_append_tlv(buf, TIPC_TLV_LINK_INFO,
Per Liden4323add2006-01-18 00:38:21 +0100681 &link_info, sizeof(link_info));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900682 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100683 }
684
685 return buf;
686}