blob: c20bd851a44a2b6a404f37415d144ae0d3c1514f [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"
Per Lidenb97bf3f2006-01-02 19:04:38 +010040#include "port.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010041#include "name_distr.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010042
David S. Miller6c000552008-09-02 23:38:32 -070043static void node_lost_contact(struct tipc_node *n_ptr);
44static void node_established_contact(struct tipc_node *n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +010045
stephen hemminger31e3c3f2010-10-13 13:20:35 +000046/* sorted list of nodes within cluster */
47static struct tipc_node *tipc_nodes = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +010048
Allan Stephens2ecb0922008-05-21 14:53:00 -070049static DEFINE_SPINLOCK(node_create_lock);
50
Per Lidenb97bf3f2006-01-02 19:04:38 +010051u32 tipc_own_tag = 0;
52
Allan Stephens2ecb0922008-05-21 14:53:00 -070053/**
54 * tipc_node_create - create neighboring node
55 *
56 * Currently, this routine is called by neighbor discovery code, which holds
57 * net_lock for reading only. We must take node_create_lock to ensure a node
58 * isn't created twice if two different bearers discover the node at the same
59 * time. (It would be preferable to switch to holding net_lock in write mode,
60 * but this is a non-trivial change.)
61 */
62
David S. Miller6c000552008-09-02 23:38:32 -070063struct tipc_node *tipc_node_create(u32 addr)
Per Lidenb97bf3f2006-01-02 19:04:38 +010064{
65 struct cluster *c_ptr;
David S. Miller6c000552008-09-02 23:38:32 -070066 struct tipc_node *n_ptr;
67 struct tipc_node **curr_node;
Per Lidenb97bf3f2006-01-02 19:04:38 +010068
Allan Stephens2ecb0922008-05-21 14:53:00 -070069 spin_lock_bh(&node_create_lock);
70
71 for (n_ptr = tipc_nodes; n_ptr; n_ptr = n_ptr->next) {
72 if (addr < n_ptr->addr)
73 break;
74 if (addr == n_ptr->addr) {
75 spin_unlock_bh(&node_create_lock);
76 return n_ptr;
77 }
78 }
79
Arnaldo Carvalho de Melo2710b572006-11-21 01:22:12 -020080 n_ptr = kzalloc(sizeof(*n_ptr),GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -070081 if (!n_ptr) {
Allan Stephens2ecb0922008-05-21 14:53:00 -070082 spin_unlock_bh(&node_create_lock);
Allan Stephensa10bd922006-06-25 23:52:17 -070083 warn("Node creation failed, no memory\n");
84 return NULL;
85 }
Per Lidenb97bf3f2006-01-02 19:04:38 +010086
Allan Stephensa10bd922006-06-25 23:52:17 -070087 c_ptr = tipc_cltr_find(addr);
88 if (!c_ptr) {
89 c_ptr = tipc_cltr_create(addr);
90 }
91 if (!c_ptr) {
Allan Stephens2ecb0922008-05-21 14:53:00 -070092 spin_unlock_bh(&node_create_lock);
Allan Stephensa10bd922006-06-25 23:52:17 -070093 kfree(n_ptr);
94 return NULL;
95 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090096
Allan Stephensa10bd922006-06-25 23:52:17 -070097 n_ptr->addr = addr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090098 spin_lock_init(&n_ptr->lock);
Allan Stephensa10bd922006-06-25 23:52:17 -070099 INIT_LIST_HEAD(&n_ptr->nsub);
100 n_ptr->owner = c_ptr;
101 tipc_cltr_attach_node(c_ptr, n_ptr);
102 n_ptr->last_router = -1;
103
104 /* Insert node into ordered list */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900105 for (curr_node = &tipc_nodes; *curr_node;
Allan Stephensa10bd922006-06-25 23:52:17 -0700106 curr_node = &(*curr_node)->next) {
107 if (addr < (*curr_node)->addr) {
108 n_ptr->next = *curr_node;
109 break;
110 }
111 }
112 (*curr_node) = n_ptr;
Allan Stephens2ecb0922008-05-21 14:53:00 -0700113 spin_unlock_bh(&node_create_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100114 return n_ptr;
115}
116
David S. Miller6c000552008-09-02 23:38:32 -0700117void tipc_node_delete(struct tipc_node *n_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100118{
119 if (!n_ptr)
120 return;
121
Per Lidenb97bf3f2006-01-02 19:04:38 +0100122 dbg("node %x deleted\n", n_ptr->addr);
123 kfree(n_ptr);
124}
125
126
127/**
Per Liden4323add2006-01-18 00:38:21 +0100128 * tipc_node_link_up - handle addition of link
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900129 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100130 * Link becomes active (alone or shared) or standby, depending on its priority.
131 */
132
David S. Miller6c000552008-09-02 23:38:32 -0700133void tipc_node_link_up(struct tipc_node *n_ptr, struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100134{
135 struct link **active = &n_ptr->active_links[0];
136
Allan Stephens5392d642006-06-25 23:52:50 -0700137 n_ptr->working_links++;
138
Per Lidenb97bf3f2006-01-02 19:04:38 +0100139 info("Established link <%s> on network plane %c\n",
140 l_ptr->name, l_ptr->b_ptr->net_plane);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900141
Per Lidenb97bf3f2006-01-02 19:04:38 +0100142 if (!active[0]) {
143 dbg(" link %x into %x/%x\n", l_ptr, &active[0], &active[1]);
144 active[0] = active[1] = l_ptr;
145 node_established_contact(n_ptr);
146 return;
147 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900148 if (l_ptr->priority < active[0]->priority) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700149 info("New link <%s> becomes standby\n", l_ptr->name);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100150 return;
151 }
Per Liden4323add2006-01-18 00:38:21 +0100152 tipc_link_send_duplicate(active[0], l_ptr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900153 if (l_ptr->priority == active[0]->priority) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100154 active[0] = l_ptr;
155 return;
156 }
Allan Stephensa10bd922006-06-25 23:52:17 -0700157 info("Old link <%s> becomes standby\n", active[0]->name);
158 if (active[1] != active[0])
159 info("Old link <%s> becomes standby\n", active[1]->name);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100160 active[0] = active[1] = l_ptr;
161}
162
163/**
164 * node_select_active_links - select active link
165 */
166
David S. Miller6c000552008-09-02 23:38:32 -0700167static void node_select_active_links(struct tipc_node *n_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100168{
169 struct link **active = &n_ptr->active_links[0];
170 u32 i;
171 u32 highest_prio = 0;
172
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900173 active[0] = active[1] = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100174
175 for (i = 0; i < MAX_BEARERS; i++) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900176 struct link *l_ptr = n_ptr->links[i];
Per Lidenb97bf3f2006-01-02 19:04:38 +0100177
Per Liden4323add2006-01-18 00:38:21 +0100178 if (!l_ptr || !tipc_link_is_up(l_ptr) ||
Per Lidenb97bf3f2006-01-02 19:04:38 +0100179 (l_ptr->priority < highest_prio))
180 continue;
181
182 if (l_ptr->priority > highest_prio) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900183 highest_prio = l_ptr->priority;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100184 active[0] = active[1] = l_ptr;
185 } else {
186 active[1] = l_ptr;
187 }
188 }
189}
190
191/**
Per Liden4323add2006-01-18 00:38:21 +0100192 * tipc_node_link_down - handle loss of link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100193 */
194
David S. Miller6c000552008-09-02 23:38:32 -0700195void tipc_node_link_down(struct tipc_node *n_ptr, struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100196{
197 struct link **active;
198
Allan Stephens5392d642006-06-25 23:52:50 -0700199 n_ptr->working_links--;
200
Per Liden4323add2006-01-18 00:38:21 +0100201 if (!tipc_link_is_active(l_ptr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100202 info("Lost standby link <%s> on network plane %c\n",
203 l_ptr->name, l_ptr->b_ptr->net_plane);
204 return;
205 }
206 info("Lost link <%s> on network plane %c\n",
207 l_ptr->name, l_ptr->b_ptr->net_plane);
208
209 active = &n_ptr->active_links[0];
210 if (active[0] == l_ptr)
211 active[0] = active[1];
212 if (active[1] == l_ptr)
213 active[1] = active[0];
214 if (active[0] == l_ptr)
215 node_select_active_links(n_ptr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900216 if (tipc_node_is_up(n_ptr))
Per Liden4323add2006-01-18 00:38:21 +0100217 tipc_link_changeover(l_ptr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900218 else
Per Lidenb97bf3f2006-01-02 19:04:38 +0100219 node_lost_contact(n_ptr);
220}
221
David S. Miller6c000552008-09-02 23:38:32 -0700222int tipc_node_has_active_links(struct tipc_node *n_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100223{
Allan Stephens76ae0d72010-08-17 11:00:12 +0000224 return n_ptr->active_links[0] != NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100225}
226
David S. Miller6c000552008-09-02 23:38:32 -0700227int tipc_node_has_redundant_links(struct tipc_node *n_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100228{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000229 return n_ptr->working_links > 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100230}
231
David S. Miller6c000552008-09-02 23:38:32 -0700232static int tipc_node_has_active_routes(struct tipc_node *n_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100233{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000234 return n_ptr && (n_ptr->last_router >= 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100235}
236
David S. Miller6c000552008-09-02 23:38:32 -0700237int tipc_node_is_up(struct tipc_node *n_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100238{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000239 return tipc_node_has_active_links(n_ptr) || tipc_node_has_active_routes(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100240}
241
David S. Miller6c000552008-09-02 23:38:32 -0700242struct tipc_node *tipc_node_attach_link(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100243{
David S. Miller6c000552008-09-02 23:38:32 -0700244 struct tipc_node *n_ptr = tipc_node_find(l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100245
246 if (!n_ptr)
Per Liden4323add2006-01-18 00:38:21 +0100247 n_ptr = tipc_node_create(l_ptr->addr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900248 if (n_ptr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100249 u32 bearer_id = l_ptr->b_ptr->identity;
250 char addr_string[16];
251
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900252 if (n_ptr->link_cnt >= 2) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900253 err("Attempt to create third link to %s\n",
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000254 tipc_addr_string_fill(addr_string, n_ptr->addr));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900255 return NULL;
256 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100257
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900258 if (!n_ptr->links[bearer_id]) {
259 n_ptr->links[bearer_id] = l_ptr;
Allan Stephens51f98a82010-12-31 18:59:16 +0000260 tipc_net.links++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900261 n_ptr->link_cnt++;
262 return n_ptr;
263 }
Frans Popa570f092010-03-24 07:57:29 +0000264 err("Attempt to establish second link on <%s> to %s\n",
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900265 l_ptr->b_ptr->publ.name,
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000266 tipc_addr_string_fill(addr_string, l_ptr->addr));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900267 }
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800268 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100269}
270
David S. Miller6c000552008-09-02 23:38:32 -0700271void tipc_node_detach_link(struct tipc_node *n_ptr, struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100272{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800273 n_ptr->links[l_ptr->b_ptr->identity] = NULL;
Allan Stephens51f98a82010-12-31 18:59:16 +0000274 tipc_net.links--;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100275 n_ptr->link_cnt--;
276}
277
278/*
279 * Routing table management - five cases to handle:
280 *
281 * 1: A link towards a zone/cluster external node comes up.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900282 * => Send a multicast message updating routing tables of all
283 * system nodes within own cluster that the new destination
284 * can be reached via this node.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100285 * (node.establishedContact()=>cluster.multicastNewRoute())
286 *
287 * 2: A link towards a slave node comes up.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900288 * => Send a multicast message updating routing tables of all
289 * system nodes within own cluster that the new destination
290 * can be reached via this node.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100291 * (node.establishedContact()=>cluster.multicastNewRoute())
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900292 * => Send a message to the slave node about existence
Per Lidenb97bf3f2006-01-02 19:04:38 +0100293 * of all system nodes within cluster:
294 * (node.establishedContact()=>cluster.sendLocalRoutes())
295 *
296 * 3: A new cluster local system node becomes available.
297 * => Send message(s) to this particular node containing
298 * information about all cluster external and slave
299 * nodes which can be reached via this node.
300 * (node.establishedContact()==>network.sendExternalRoutes())
301 * (node.establishedContact()==>network.sendSlaveRoutes())
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900302 * => Send messages to all directly connected slave nodes
Per Lidenb97bf3f2006-01-02 19:04:38 +0100303 * containing information about the existence of the new node
304 * (node.establishedContact()=>cluster.multicastNewRoute())
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900305 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100306 * 4: The link towards a zone/cluster external node or slave
307 * node goes down.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900308 * => Send a multcast message updating routing tables of all
Per Lidenb97bf3f2006-01-02 19:04:38 +0100309 * nodes within cluster that the new destination can not any
310 * longer be reached via this node.
311 * (node.lostAllLinks()=>cluster.bcastLostRoute())
312 *
313 * 5: A cluster local system node becomes unavailable.
314 * => Remove all references to this node from the local
315 * routing tables. Note: This is a completely node
316 * local operation.
317 * (node.lostAllLinks()=>network.removeAsRouter())
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900318 * => Send messages to all directly connected slave nodes
Per Lidenb97bf3f2006-01-02 19:04:38 +0100319 * containing information about loss of the node
320 * (node.establishedContact()=>cluster.multicastLostRoute())
321 *
322 */
323
David S. Miller6c000552008-09-02 23:38:32 -0700324static void node_established_contact(struct tipc_node *n_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100325{
326 struct cluster *c_ptr;
327
328 dbg("node_established_contact:-> %x\n", n_ptr->addr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900329 if (!tipc_node_has_active_routes(n_ptr) && in_own_cluster(n_ptr->addr)) {
Per Liden4323add2006-01-18 00:38:21 +0100330 tipc_k_signal((Handler)tipc_named_node_up, n_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100331 }
332
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900333 /* Syncronize broadcast acks */
334 n_ptr->bclink.acked = tipc_bclink_get_last_sent();
Per Lidenb97bf3f2006-01-02 19:04:38 +0100335
336 if (is_slave(tipc_own_addr))
337 return;
338 if (!in_own_cluster(n_ptr->addr)) {
339 /* Usage case 1 (see above) */
Per Liden4323add2006-01-18 00:38:21 +0100340 c_ptr = tipc_cltr_find(tipc_own_addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100341 if (!c_ptr)
Per Liden4323add2006-01-18 00:38:21 +0100342 c_ptr = tipc_cltr_create(tipc_own_addr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900343 if (c_ptr)
344 tipc_cltr_bcast_new_route(c_ptr, n_ptr->addr, 1,
Per Liden4323add2006-01-18 00:38:21 +0100345 tipc_max_nodes);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100346 return;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900347 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100348
349 c_ptr = n_ptr->owner;
350 if (is_slave(n_ptr->addr)) {
351 /* Usage case 2 (see above) */
Per Liden4323add2006-01-18 00:38:21 +0100352 tipc_cltr_bcast_new_route(c_ptr, n_ptr->addr, 1, tipc_max_nodes);
353 tipc_cltr_send_local_routes(c_ptr, n_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100354 return;
355 }
356
357 if (n_ptr->bclink.supported) {
Per Liden4323add2006-01-18 00:38:21 +0100358 tipc_nmap_add(&tipc_cltr_bcast_nodes, n_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100359 if (n_ptr->addr < tipc_own_addr)
360 tipc_own_tag++;
361 }
362
363 /* Case 3 (see above) */
Per Liden4323add2006-01-18 00:38:21 +0100364 tipc_net_send_external_routes(n_ptr->addr);
365 tipc_cltr_send_slave_routes(c_ptr, n_ptr->addr);
366 tipc_cltr_bcast_new_route(c_ptr, n_ptr->addr, LOWEST_SLAVE,
367 tipc_highest_allowed_slave);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100368}
369
Allan Stephens5a68d5e2010-08-17 11:00:16 +0000370static void node_cleanup_finished(unsigned long node_addr)
371{
372 struct tipc_node *n_ptr;
373
374 read_lock_bh(&tipc_net_lock);
375 n_ptr = tipc_node_find(node_addr);
376 if (n_ptr) {
377 tipc_node_lock(n_ptr);
378 n_ptr->cleanup_required = 0;
379 tipc_node_unlock(n_ptr);
380 }
381 read_unlock_bh(&tipc_net_lock);
382}
383
David S. Miller6c000552008-09-02 23:38:32 -0700384static void node_lost_contact(struct tipc_node *n_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100385{
386 struct cluster *c_ptr;
David S. Miller6c000552008-09-02 23:38:32 -0700387 struct tipc_node_subscr *ns, *tns;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100388 char addr_string[16];
389 u32 i;
390
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900391 /* Clean up broadcast reception remains */
392 n_ptr->bclink.gap_after = n_ptr->bclink.gap_to = 0;
393 while (n_ptr->bclink.deferred_head) {
394 struct sk_buff* buf = n_ptr->bclink.deferred_head;
395 n_ptr->bclink.deferred_head = buf->next;
396 buf_discard(buf);
397 }
398 if (n_ptr->bclink.defragm) {
399 buf_discard(n_ptr->bclink.defragm);
400 n_ptr->bclink.defragm = NULL;
401 }
402 if (in_own_cluster(n_ptr->addr) && n_ptr->bclink.supported) {
403 tipc_bclink_acknowledge(n_ptr, mod(n_ptr->bclink.acked + 10000));
404 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100405
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900406 /* Update routing tables */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100407 if (is_slave(tipc_own_addr)) {
Per Liden4323add2006-01-18 00:38:21 +0100408 tipc_net_remove_as_router(n_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100409 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900410 if (!in_own_cluster(n_ptr->addr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100411 /* Case 4 (see above) */
Per Liden4323add2006-01-18 00:38:21 +0100412 c_ptr = tipc_cltr_find(tipc_own_addr);
413 tipc_cltr_bcast_lost_route(c_ptr, n_ptr->addr, 1,
414 tipc_max_nodes);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100415 } else {
416 /* Case 5 (see above) */
Per Liden4323add2006-01-18 00:38:21 +0100417 c_ptr = tipc_cltr_find(n_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100418 if (is_slave(n_ptr->addr)) {
Per Liden4323add2006-01-18 00:38:21 +0100419 tipc_cltr_bcast_lost_route(c_ptr, n_ptr->addr, 1,
420 tipc_max_nodes);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100421 } else {
422 if (n_ptr->bclink.supported) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900423 tipc_nmap_remove(&tipc_cltr_bcast_nodes,
Per Liden4323add2006-01-18 00:38:21 +0100424 n_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100425 if (n_ptr->addr < tipc_own_addr)
426 tipc_own_tag--;
427 }
Per Liden4323add2006-01-18 00:38:21 +0100428 tipc_net_remove_as_router(n_ptr->addr);
429 tipc_cltr_bcast_lost_route(c_ptr, n_ptr->addr,
430 LOWEST_SLAVE,
431 tipc_highest_allowed_slave);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100432 }
433 }
434 }
Per Liden4323add2006-01-18 00:38:21 +0100435 if (tipc_node_has_active_routes(n_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100436 return;
437
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900438 info("Lost contact with %s\n",
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000439 tipc_addr_string_fill(addr_string, n_ptr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100440
441 /* Abort link changeover */
442 for (i = 0; i < MAX_BEARERS; i++) {
443 struct link *l_ptr = n_ptr->links[i];
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900444 if (!l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100445 continue;
446 l_ptr->reset_checkpoint = l_ptr->next_in_no;
447 l_ptr->exp_msg_count = 0;
Per Liden4323add2006-01-18 00:38:21 +0100448 tipc_link_reset_fragments(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100449 }
450
451 /* Notify subscribers */
452 list_for_each_entry_safe(ns, tns, &n_ptr->nsub, nodesub_list) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900453 ns->node = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100454 list_del_init(&ns->nodesub_list);
Per Liden4323add2006-01-18 00:38:21 +0100455 tipc_k_signal((Handler)ns->handle_node_down,
456 (unsigned long)ns->usr_handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100457 }
Allan Stephens5a68d5e2010-08-17 11:00:16 +0000458
459 /* Prevent re-contact with node until all cleanup is done */
460
461 n_ptr->cleanup_required = 1;
462 tipc_k_signal((Handler)node_cleanup_finished, n_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100463}
464
465/**
Per Liden4323add2006-01-18 00:38:21 +0100466 * tipc_node_select_next_hop - find the next-hop node for a message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900467 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100468 * Called by when cluster local lookup has failed.
469 */
470
David S. Miller6c000552008-09-02 23:38:32 -0700471struct tipc_node *tipc_node_select_next_hop(u32 addr, u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100472{
David S. Miller6c000552008-09-02 23:38:32 -0700473 struct tipc_node *n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100474 u32 router_addr;
475
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900476 if (!tipc_addr_domain_valid(addr))
477 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100478
479 /* Look for direct link to destination processsor */
Per Liden4323add2006-01-18 00:38:21 +0100480 n_ptr = tipc_node_find(addr);
481 if (n_ptr && tipc_node_has_active_links(n_ptr))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900482 return n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100483
484 /* Cluster local system nodes *must* have direct links */
485 if (!is_slave(addr) && in_own_cluster(addr))
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800486 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100487
488 /* Look for cluster local router with direct link to node */
Per Liden4323add2006-01-18 00:38:21 +0100489 router_addr = tipc_node_select_router(n_ptr, selector);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900490 if (router_addr)
491 return tipc_node_select(router_addr, selector);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100492
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900493 /* Slave nodes can only be accessed within own cluster via a
Per Lidenb97bf3f2006-01-02 19:04:38 +0100494 known router with direct link -- if no router was found,give up */
495 if (is_slave(addr))
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800496 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100497
498 /* Inter zone/cluster -- find any direct link to remote cluster */
499 addr = tipc_addr(tipc_zone(addr), tipc_cluster(addr), 0);
Per Liden4323add2006-01-18 00:38:21 +0100500 n_ptr = tipc_net_select_remote_node(addr, selector);
501 if (n_ptr && tipc_node_has_active_links(n_ptr))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900502 return n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100503
504 /* Last resort -- look for any router to anywhere in remote zone */
Per Liden4323add2006-01-18 00:38:21 +0100505 router_addr = tipc_net_select_router(addr, selector);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900506 if (router_addr)
507 return tipc_node_select(router_addr, selector);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100508
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900509 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100510}
511
512/**
Per Liden4323add2006-01-18 00:38:21 +0100513 * tipc_node_select_router - select router to reach specified node
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900514 *
515 * Uses a deterministic and fair algorithm for selecting router node.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100516 */
517
David S. Miller6c000552008-09-02 23:38:32 -0700518u32 tipc_node_select_router(struct tipc_node *n_ptr, u32 ref)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100519{
520 u32 ulim;
521 u32 mask;
522 u32 start;
523 u32 r;
524
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900525 if (!n_ptr)
526 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100527
528 if (n_ptr->last_router < 0)
529 return 0;
530 ulim = ((n_ptr->last_router + 1) * 32) - 1;
531
532 /* Start entry must be random */
533 mask = tipc_max_nodes;
534 while (mask > ulim)
535 mask >>= 1;
536 start = ref & mask;
537 r = start;
538
539 /* Lookup upwards with wrap-around */
540 do {
541 if (((n_ptr->routers[r / 32]) >> (r % 32)) & 1)
542 break;
543 } while (++r <= ulim);
544 if (r > ulim) {
545 r = 1;
546 do {
547 if (((n_ptr->routers[r / 32]) >> (r % 32)) & 1)
548 break;
549 } while (++r < start);
550 assert(r != start);
551 }
552 assert(r && (r <= ulim));
553 return tipc_addr(own_zone(), own_cluster(), r);
554}
555
David S. Miller6c000552008-09-02 23:38:32 -0700556void tipc_node_add_router(struct tipc_node *n_ptr, u32 router)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100557{
558 u32 r_num = tipc_node(router);
559
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900560 n_ptr->routers[r_num / 32] =
Per Lidenb97bf3f2006-01-02 19:04:38 +0100561 ((1 << (r_num % 32)) | n_ptr->routers[r_num / 32]);
562 n_ptr->last_router = tipc_max_nodes / 32;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900563 while ((--n_ptr->last_router >= 0) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +0100564 !n_ptr->routers[n_ptr->last_router]);
565}
566
David S. Miller6c000552008-09-02 23:38:32 -0700567void tipc_node_remove_router(struct tipc_node *n_ptr, u32 router)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100568{
569 u32 r_num = tipc_node(router);
570
571 if (n_ptr->last_router < 0)
572 return; /* No routes */
573
574 n_ptr->routers[r_num / 32] =
575 ((~(1 << (r_num % 32))) & (n_ptr->routers[r_num / 32]));
576 n_ptr->last_router = tipc_max_nodes / 32;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900577 while ((--n_ptr->last_router >= 0) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +0100578 !n_ptr->routers[n_ptr->last_router]);
579
Per Liden4323add2006-01-18 00:38:21 +0100580 if (!tipc_node_is_up(n_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100581 node_lost_contact(n_ptr);
582}
583
Per Liden4323add2006-01-18 00:38:21 +0100584struct sk_buff *tipc_node_get_nodes(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100585{
586 u32 domain;
587 struct sk_buff *buf;
David S. Miller6c000552008-09-02 23:38:32 -0700588 struct tipc_node *n_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900589 struct tipc_node_info node_info;
Allan Stephensea138472006-06-29 12:33:20 -0700590 u32 payload_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100591
592 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NET_ADDR))
Per Liden4323add2006-01-18 00:38:21 +0100593 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100594
Al Viro3e6c8cd2006-11-08 00:19:09 -0800595 domain = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
Per Liden4323add2006-01-18 00:38:21 +0100596 if (!tipc_addr_domain_valid(domain))
597 return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
598 " (network address)");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100599
Allan Stephens1aad72d2008-07-14 22:44:58 -0700600 read_lock_bh(&tipc_net_lock);
601 if (!tipc_nodes) {
602 read_unlock_bh(&tipc_net_lock);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900603 return tipc_cfg_reply_none();
Allan Stephens1aad72d2008-07-14 22:44:58 -0700604 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100605
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900606 /* For now, get space for all other nodes
Per Lidenb97bf3f2006-01-02 19:04:38 +0100607 (will need to modify this when slave nodes are supported */
608
Allan Stephensea138472006-06-29 12:33:20 -0700609 payload_size = TLV_SPACE(sizeof(node_info)) * (tipc_max_nodes - 1);
Allan Stephens1aad72d2008-07-14 22:44:58 -0700610 if (payload_size > 32768u) {
611 read_unlock_bh(&tipc_net_lock);
Allan Stephensea138472006-06-29 12:33:20 -0700612 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
613 " (too many nodes)");
Allan Stephens1aad72d2008-07-14 22:44:58 -0700614 }
Allan Stephensea138472006-06-29 12:33:20 -0700615 buf = tipc_cfg_reply_alloc(payload_size);
Allan Stephens1aad72d2008-07-14 22:44:58 -0700616 if (!buf) {
617 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100618 return NULL;
Allan Stephens1aad72d2008-07-14 22:44:58 -0700619 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100620
621 /* Add TLVs for all nodes in scope */
622
Per Liden4323add2006-01-18 00:38:21 +0100623 for (n_ptr = tipc_nodes; n_ptr; n_ptr = n_ptr->next) {
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000624 if (!tipc_in_scope(domain, n_ptr->addr))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100625 continue;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900626 node_info.addr = htonl(n_ptr->addr);
627 node_info.up = htonl(tipc_node_is_up(n_ptr));
628 tipc_cfg_append_tlv(buf, TIPC_TLV_NODE_INFO,
Per Liden4323add2006-01-18 00:38:21 +0100629 &node_info, sizeof(node_info));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100630 }
631
Allan Stephens1aad72d2008-07-14 22:44:58 -0700632 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100633 return buf;
634}
635
Per Liden4323add2006-01-18 00:38:21 +0100636struct sk_buff *tipc_node_get_links(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100637{
638 u32 domain;
639 struct sk_buff *buf;
David S. Miller6c000552008-09-02 23:38:32 -0700640 struct tipc_node *n_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900641 struct tipc_link_info link_info;
Allan Stephensea138472006-06-29 12:33:20 -0700642 u32 payload_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100643
644 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NET_ADDR))
Per Liden4323add2006-01-18 00:38:21 +0100645 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100646
Al Viro3e6c8cd2006-11-08 00:19:09 -0800647 domain = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
Per Liden4323add2006-01-18 00:38:21 +0100648 if (!tipc_addr_domain_valid(domain))
649 return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
650 " (network address)");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100651
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900652 if (tipc_mode != TIPC_NET_MODE)
653 return tipc_cfg_reply_none();
654
Allan Stephens1aad72d2008-07-14 22:44:58 -0700655 read_lock_bh(&tipc_net_lock);
656
Allan Stephensea138472006-06-29 12:33:20 -0700657 /* Get space for all unicast links + multicast link */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100658
Allan Stephens51f98a82010-12-31 18:59:16 +0000659 payload_size = TLV_SPACE(sizeof(link_info)) * (tipc_net.links + 1);
Allan Stephens1aad72d2008-07-14 22:44:58 -0700660 if (payload_size > 32768u) {
661 read_unlock_bh(&tipc_net_lock);
Allan Stephensea138472006-06-29 12:33:20 -0700662 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
663 " (too many links)");
Allan Stephens1aad72d2008-07-14 22:44:58 -0700664 }
Allan Stephensea138472006-06-29 12:33:20 -0700665 buf = tipc_cfg_reply_alloc(payload_size);
Allan Stephens1aad72d2008-07-14 22:44:58 -0700666 if (!buf) {
667 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100668 return NULL;
Allan Stephens1aad72d2008-07-14 22:44:58 -0700669 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100670
671 /* Add TLV for broadcast link */
672
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900673 link_info.dest = htonl(tipc_own_addr & 0xfffff00);
674 link_info.up = htonl(1);
Stephen Hemminger4b704d52009-03-18 19:11:29 -0700675 strlcpy(link_info.str, tipc_bclink_name, TIPC_MAX_LINK_NAME);
Per Liden4323add2006-01-18 00:38:21 +0100676 tipc_cfg_append_tlv(buf, TIPC_TLV_LINK_INFO, &link_info, sizeof(link_info));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100677
678 /* Add TLVs for any other links in scope */
679
Per Liden4323add2006-01-18 00:38:21 +0100680 for (n_ptr = tipc_nodes; n_ptr; n_ptr = n_ptr->next) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900681 u32 i;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100682
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000683 if (!tipc_in_scope(domain, n_ptr->addr))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100684 continue;
Allan Stephens1aad72d2008-07-14 22:44:58 -0700685 tipc_node_lock(n_ptr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900686 for (i = 0; i < MAX_BEARERS; i++) {
687 if (!n_ptr->links[i])
688 continue;
689 link_info.dest = htonl(n_ptr->addr);
690 link_info.up = htonl(tipc_link_is_up(n_ptr->links[i]));
691 strcpy(link_info.str, n_ptr->links[i]->name);
692 tipc_cfg_append_tlv(buf, TIPC_TLV_LINK_INFO,
Per Liden4323add2006-01-18 00:38:21 +0100693 &link_info, sizeof(link_info));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900694 }
Allan Stephens1aad72d2008-07-14 22:44:58 -0700695 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100696 }
697
Allan Stephens1aad72d2008-07-14 22:44:58 -0700698 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100699 return buf;
700}