blob: b4d87eb2dc5d871352d6d1f54ea73df830d83779 [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
David S. Miller6c000552008-09-02 23:38:32 -070049void node_print(struct print_buf *buf, struct tipc_node *n_ptr, char *str);
50static void node_lost_contact(struct tipc_node *n_ptr);
51static void node_established_contact(struct tipc_node *n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +010052
stephen hemminger31e3c3f2010-10-13 13:20:35 +000053/* sorted list of nodes within cluster */
54static struct tipc_node *tipc_nodes = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +010055
Allan Stephens2ecb0922008-05-21 14:53:00 -070056static DEFINE_SPINLOCK(node_create_lock);
57
Per Lidenb97bf3f2006-01-02 19:04:38 +010058u32 tipc_own_tag = 0;
59
Allan Stephens2ecb0922008-05-21 14:53:00 -070060/**
61 * tipc_node_create - create neighboring node
62 *
63 * Currently, this routine is called by neighbor discovery code, which holds
64 * net_lock for reading only. We must take node_create_lock to ensure a node
65 * isn't created twice if two different bearers discover the node at the same
66 * time. (It would be preferable to switch to holding net_lock in write mode,
67 * but this is a non-trivial change.)
68 */
69
David S. Miller6c000552008-09-02 23:38:32 -070070struct tipc_node *tipc_node_create(u32 addr)
Per Lidenb97bf3f2006-01-02 19:04:38 +010071{
72 struct cluster *c_ptr;
David S. Miller6c000552008-09-02 23:38:32 -070073 struct tipc_node *n_ptr;
74 struct tipc_node **curr_node;
Per Lidenb97bf3f2006-01-02 19:04:38 +010075
Allan Stephens2ecb0922008-05-21 14:53:00 -070076 spin_lock_bh(&node_create_lock);
77
78 for (n_ptr = tipc_nodes; n_ptr; n_ptr = n_ptr->next) {
79 if (addr < n_ptr->addr)
80 break;
81 if (addr == n_ptr->addr) {
82 spin_unlock_bh(&node_create_lock);
83 return n_ptr;
84 }
85 }
86
Arnaldo Carvalho de Melo2710b572006-11-21 01:22:12 -020087 n_ptr = kzalloc(sizeof(*n_ptr),GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -070088 if (!n_ptr) {
Allan Stephens2ecb0922008-05-21 14:53:00 -070089 spin_unlock_bh(&node_create_lock);
Allan Stephensa10bd922006-06-25 23:52:17 -070090 warn("Node creation failed, no memory\n");
91 return NULL;
92 }
Per Lidenb97bf3f2006-01-02 19:04:38 +010093
Allan Stephensa10bd922006-06-25 23:52:17 -070094 c_ptr = tipc_cltr_find(addr);
95 if (!c_ptr) {
96 c_ptr = tipc_cltr_create(addr);
97 }
98 if (!c_ptr) {
Allan Stephens2ecb0922008-05-21 14:53:00 -070099 spin_unlock_bh(&node_create_lock);
Allan Stephensa10bd922006-06-25 23:52:17 -0700100 kfree(n_ptr);
101 return NULL;
102 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900103
Allan Stephensa10bd922006-06-25 23:52:17 -0700104 n_ptr->addr = addr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900105 spin_lock_init(&n_ptr->lock);
Allan Stephensa10bd922006-06-25 23:52:17 -0700106 INIT_LIST_HEAD(&n_ptr->nsub);
107 n_ptr->owner = c_ptr;
108 tipc_cltr_attach_node(c_ptr, n_ptr);
109 n_ptr->last_router = -1;
110
111 /* Insert node into ordered list */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900112 for (curr_node = &tipc_nodes; *curr_node;
Allan Stephensa10bd922006-06-25 23:52:17 -0700113 curr_node = &(*curr_node)->next) {
114 if (addr < (*curr_node)->addr) {
115 n_ptr->next = *curr_node;
116 break;
117 }
118 }
119 (*curr_node) = n_ptr;
Allan Stephens2ecb0922008-05-21 14:53:00 -0700120 spin_unlock_bh(&node_create_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100121 return n_ptr;
122}
123
David S. Miller6c000552008-09-02 23:38:32 -0700124void tipc_node_delete(struct tipc_node *n_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100125{
126 if (!n_ptr)
127 return;
128
Per Lidenb97bf3f2006-01-02 19:04:38 +0100129 dbg("node %x deleted\n", n_ptr->addr);
130 kfree(n_ptr);
131}
132
133
134/**
Per Liden4323add2006-01-18 00:38:21 +0100135 * tipc_node_link_up - handle addition of link
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900136 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100137 * Link becomes active (alone or shared) or standby, depending on its priority.
138 */
139
David S. Miller6c000552008-09-02 23:38:32 -0700140void tipc_node_link_up(struct tipc_node *n_ptr, struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100141{
142 struct link **active = &n_ptr->active_links[0];
143
Allan Stephens5392d642006-06-25 23:52:50 -0700144 n_ptr->working_links++;
145
Per Lidenb97bf3f2006-01-02 19:04:38 +0100146 info("Established link <%s> on network plane %c\n",
147 l_ptr->name, l_ptr->b_ptr->net_plane);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900148
Per Lidenb97bf3f2006-01-02 19:04:38 +0100149 if (!active[0]) {
150 dbg(" link %x into %x/%x\n", l_ptr, &active[0], &active[1]);
151 active[0] = active[1] = l_ptr;
152 node_established_contact(n_ptr);
153 return;
154 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900155 if (l_ptr->priority < active[0]->priority) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700156 info("New link <%s> becomes standby\n", l_ptr->name);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100157 return;
158 }
Per Liden4323add2006-01-18 00:38:21 +0100159 tipc_link_send_duplicate(active[0], l_ptr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900160 if (l_ptr->priority == active[0]->priority) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100161 active[0] = l_ptr;
162 return;
163 }
Allan Stephensa10bd922006-06-25 23:52:17 -0700164 info("Old link <%s> becomes standby\n", active[0]->name);
165 if (active[1] != active[0])
166 info("Old link <%s> becomes standby\n", active[1]->name);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100167 active[0] = active[1] = l_ptr;
168}
169
170/**
171 * node_select_active_links - select active link
172 */
173
David S. Miller6c000552008-09-02 23:38:32 -0700174static void node_select_active_links(struct tipc_node *n_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100175{
176 struct link **active = &n_ptr->active_links[0];
177 u32 i;
178 u32 highest_prio = 0;
179
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900180 active[0] = active[1] = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100181
182 for (i = 0; i < MAX_BEARERS; i++) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900183 struct link *l_ptr = n_ptr->links[i];
Per Lidenb97bf3f2006-01-02 19:04:38 +0100184
Per Liden4323add2006-01-18 00:38:21 +0100185 if (!l_ptr || !tipc_link_is_up(l_ptr) ||
Per Lidenb97bf3f2006-01-02 19:04:38 +0100186 (l_ptr->priority < highest_prio))
187 continue;
188
189 if (l_ptr->priority > highest_prio) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900190 highest_prio = l_ptr->priority;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100191 active[0] = active[1] = l_ptr;
192 } else {
193 active[1] = l_ptr;
194 }
195 }
196}
197
198/**
Per Liden4323add2006-01-18 00:38:21 +0100199 * tipc_node_link_down - handle loss of link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100200 */
201
David S. Miller6c000552008-09-02 23:38:32 -0700202void tipc_node_link_down(struct tipc_node *n_ptr, struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100203{
204 struct link **active;
205
Allan Stephens5392d642006-06-25 23:52:50 -0700206 n_ptr->working_links--;
207
Per Liden4323add2006-01-18 00:38:21 +0100208 if (!tipc_link_is_active(l_ptr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100209 info("Lost standby link <%s> on network plane %c\n",
210 l_ptr->name, l_ptr->b_ptr->net_plane);
211 return;
212 }
213 info("Lost link <%s> on network plane %c\n",
214 l_ptr->name, l_ptr->b_ptr->net_plane);
215
216 active = &n_ptr->active_links[0];
217 if (active[0] == l_ptr)
218 active[0] = active[1];
219 if (active[1] == l_ptr)
220 active[1] = active[0];
221 if (active[0] == l_ptr)
222 node_select_active_links(n_ptr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900223 if (tipc_node_is_up(n_ptr))
Per Liden4323add2006-01-18 00:38:21 +0100224 tipc_link_changeover(l_ptr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900225 else
Per Lidenb97bf3f2006-01-02 19:04:38 +0100226 node_lost_contact(n_ptr);
227}
228
David S. Miller6c000552008-09-02 23:38:32 -0700229int tipc_node_has_active_links(struct tipc_node *n_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100230{
Allan Stephens76ae0d72010-08-17 11:00:12 +0000231 return n_ptr->active_links[0] != NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100232}
233
David S. Miller6c000552008-09-02 23:38:32 -0700234int tipc_node_has_redundant_links(struct tipc_node *n_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100235{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000236 return n_ptr->working_links > 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100237}
238
David S. Miller6c000552008-09-02 23:38:32 -0700239static int tipc_node_has_active_routes(struct tipc_node *n_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100240{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000241 return n_ptr && (n_ptr->last_router >= 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100242}
243
David S. Miller6c000552008-09-02 23:38:32 -0700244int tipc_node_is_up(struct tipc_node *n_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100245{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000246 return tipc_node_has_active_links(n_ptr) || tipc_node_has_active_routes(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100247}
248
David S. Miller6c000552008-09-02 23:38:32 -0700249struct tipc_node *tipc_node_attach_link(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100250{
David S. Miller6c000552008-09-02 23:38:32 -0700251 struct tipc_node *n_ptr = tipc_node_find(l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100252
253 if (!n_ptr)
Per Liden4323add2006-01-18 00:38:21 +0100254 n_ptr = tipc_node_create(l_ptr->addr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900255 if (n_ptr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100256 u32 bearer_id = l_ptr->b_ptr->identity;
257 char addr_string[16];
258
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900259 if (n_ptr->link_cnt >= 2) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900260 err("Attempt to create third link to %s\n",
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000261 tipc_addr_string_fill(addr_string, n_ptr->addr));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900262 return NULL;
263 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100264
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900265 if (!n_ptr->links[bearer_id]) {
266 n_ptr->links[bearer_id] = l_ptr;
267 tipc_net.zones[tipc_zone(l_ptr->addr)]->links++;
268 n_ptr->link_cnt++;
269 return n_ptr;
270 }
Frans Popa570f092010-03-24 07:57:29 +0000271 err("Attempt to establish second link on <%s> to %s\n",
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900272 l_ptr->b_ptr->publ.name,
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000273 tipc_addr_string_fill(addr_string, l_ptr->addr));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900274 }
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800275 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100276}
277
David S. Miller6c000552008-09-02 23:38:32 -0700278void tipc_node_detach_link(struct tipc_node *n_ptr, struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100279{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800280 n_ptr->links[l_ptr->b_ptr->identity] = NULL;
Per Liden4323add2006-01-18 00:38:21 +0100281 tipc_net.zones[tipc_zone(l_ptr->addr)]->links--;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100282 n_ptr->link_cnt--;
283}
284
285/*
286 * Routing table management - five cases to handle:
287 *
288 * 1: A link towards a zone/cluster external node comes up.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900289 * => Send a multicast message updating routing tables of all
290 * system nodes within own cluster that the new destination
291 * can be reached via this node.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100292 * (node.establishedContact()=>cluster.multicastNewRoute())
293 *
294 * 2: A link towards a slave node comes up.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900295 * => Send a multicast message updating routing tables of all
296 * system nodes within own cluster that the new destination
297 * can be reached via this node.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100298 * (node.establishedContact()=>cluster.multicastNewRoute())
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900299 * => Send a message to the slave node about existence
Per Lidenb97bf3f2006-01-02 19:04:38 +0100300 * of all system nodes within cluster:
301 * (node.establishedContact()=>cluster.sendLocalRoutes())
302 *
303 * 3: A new cluster local system node becomes available.
304 * => Send message(s) to this particular node containing
305 * information about all cluster external and slave
306 * nodes which can be reached via this node.
307 * (node.establishedContact()==>network.sendExternalRoutes())
308 * (node.establishedContact()==>network.sendSlaveRoutes())
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 the existence of the new node
311 * (node.establishedContact()=>cluster.multicastNewRoute())
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900312 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100313 * 4: The link towards a zone/cluster external node or slave
314 * node goes down.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900315 * => Send a multcast message updating routing tables of all
Per Lidenb97bf3f2006-01-02 19:04:38 +0100316 * nodes within cluster that the new destination can not any
317 * longer be reached via this node.
318 * (node.lostAllLinks()=>cluster.bcastLostRoute())
319 *
320 * 5: A cluster local system node becomes unavailable.
321 * => Remove all references to this node from the local
322 * routing tables. Note: This is a completely node
323 * local operation.
324 * (node.lostAllLinks()=>network.removeAsRouter())
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900325 * => Send messages to all directly connected slave nodes
Per Lidenb97bf3f2006-01-02 19:04:38 +0100326 * containing information about loss of the node
327 * (node.establishedContact()=>cluster.multicastLostRoute())
328 *
329 */
330
David S. Miller6c000552008-09-02 23:38:32 -0700331static void node_established_contact(struct tipc_node *n_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100332{
333 struct cluster *c_ptr;
334
335 dbg("node_established_contact:-> %x\n", n_ptr->addr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900336 if (!tipc_node_has_active_routes(n_ptr) && in_own_cluster(n_ptr->addr)) {
Per Liden4323add2006-01-18 00:38:21 +0100337 tipc_k_signal((Handler)tipc_named_node_up, n_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100338 }
339
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900340 /* Syncronize broadcast acks */
341 n_ptr->bclink.acked = tipc_bclink_get_last_sent();
Per Lidenb97bf3f2006-01-02 19:04:38 +0100342
343 if (is_slave(tipc_own_addr))
344 return;
345 if (!in_own_cluster(n_ptr->addr)) {
346 /* Usage case 1 (see above) */
Per Liden4323add2006-01-18 00:38:21 +0100347 c_ptr = tipc_cltr_find(tipc_own_addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100348 if (!c_ptr)
Per Liden4323add2006-01-18 00:38:21 +0100349 c_ptr = tipc_cltr_create(tipc_own_addr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900350 if (c_ptr)
351 tipc_cltr_bcast_new_route(c_ptr, n_ptr->addr, 1,
Per Liden4323add2006-01-18 00:38:21 +0100352 tipc_max_nodes);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100353 return;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900354 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100355
356 c_ptr = n_ptr->owner;
357 if (is_slave(n_ptr->addr)) {
358 /* Usage case 2 (see above) */
Per Liden4323add2006-01-18 00:38:21 +0100359 tipc_cltr_bcast_new_route(c_ptr, n_ptr->addr, 1, tipc_max_nodes);
360 tipc_cltr_send_local_routes(c_ptr, n_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100361 return;
362 }
363
364 if (n_ptr->bclink.supported) {
Per Liden4323add2006-01-18 00:38:21 +0100365 tipc_nmap_add(&tipc_cltr_bcast_nodes, n_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100366 if (n_ptr->addr < tipc_own_addr)
367 tipc_own_tag++;
368 }
369
370 /* Case 3 (see above) */
Per Liden4323add2006-01-18 00:38:21 +0100371 tipc_net_send_external_routes(n_ptr->addr);
372 tipc_cltr_send_slave_routes(c_ptr, n_ptr->addr);
373 tipc_cltr_bcast_new_route(c_ptr, n_ptr->addr, LOWEST_SLAVE,
374 tipc_highest_allowed_slave);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100375}
376
Allan Stephens5a68d5ee2010-08-17 11:00:16 +0000377static void node_cleanup_finished(unsigned long node_addr)
378{
379 struct tipc_node *n_ptr;
380
381 read_lock_bh(&tipc_net_lock);
382 n_ptr = tipc_node_find(node_addr);
383 if (n_ptr) {
384 tipc_node_lock(n_ptr);
385 n_ptr->cleanup_required = 0;
386 tipc_node_unlock(n_ptr);
387 }
388 read_unlock_bh(&tipc_net_lock);
389}
390
David S. Miller6c000552008-09-02 23:38:32 -0700391static void node_lost_contact(struct tipc_node *n_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100392{
393 struct cluster *c_ptr;
David S. Miller6c000552008-09-02 23:38:32 -0700394 struct tipc_node_subscr *ns, *tns;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100395 char addr_string[16];
396 u32 i;
397
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900398 /* Clean up broadcast reception remains */
399 n_ptr->bclink.gap_after = n_ptr->bclink.gap_to = 0;
400 while (n_ptr->bclink.deferred_head) {
401 struct sk_buff* buf = n_ptr->bclink.deferred_head;
402 n_ptr->bclink.deferred_head = buf->next;
403 buf_discard(buf);
404 }
405 if (n_ptr->bclink.defragm) {
406 buf_discard(n_ptr->bclink.defragm);
407 n_ptr->bclink.defragm = NULL;
408 }
409 if (in_own_cluster(n_ptr->addr) && n_ptr->bclink.supported) {
410 tipc_bclink_acknowledge(n_ptr, mod(n_ptr->bclink.acked + 10000));
411 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100412
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900413 /* Update routing tables */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100414 if (is_slave(tipc_own_addr)) {
Per Liden4323add2006-01-18 00:38:21 +0100415 tipc_net_remove_as_router(n_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100416 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900417 if (!in_own_cluster(n_ptr->addr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100418 /* Case 4 (see above) */
Per Liden4323add2006-01-18 00:38:21 +0100419 c_ptr = tipc_cltr_find(tipc_own_addr);
420 tipc_cltr_bcast_lost_route(c_ptr, n_ptr->addr, 1,
421 tipc_max_nodes);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100422 } else {
423 /* Case 5 (see above) */
Per Liden4323add2006-01-18 00:38:21 +0100424 c_ptr = tipc_cltr_find(n_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100425 if (is_slave(n_ptr->addr)) {
Per Liden4323add2006-01-18 00:38:21 +0100426 tipc_cltr_bcast_lost_route(c_ptr, n_ptr->addr, 1,
427 tipc_max_nodes);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100428 } else {
429 if (n_ptr->bclink.supported) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900430 tipc_nmap_remove(&tipc_cltr_bcast_nodes,
Per Liden4323add2006-01-18 00:38:21 +0100431 n_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100432 if (n_ptr->addr < tipc_own_addr)
433 tipc_own_tag--;
434 }
Per Liden4323add2006-01-18 00:38:21 +0100435 tipc_net_remove_as_router(n_ptr->addr);
436 tipc_cltr_bcast_lost_route(c_ptr, n_ptr->addr,
437 LOWEST_SLAVE,
438 tipc_highest_allowed_slave);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100439 }
440 }
441 }
Per Liden4323add2006-01-18 00:38:21 +0100442 if (tipc_node_has_active_routes(n_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100443 return;
444
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900445 info("Lost contact with %s\n",
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000446 tipc_addr_string_fill(addr_string, n_ptr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100447
448 /* Abort link changeover */
449 for (i = 0; i < MAX_BEARERS; i++) {
450 struct link *l_ptr = n_ptr->links[i];
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900451 if (!l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100452 continue;
453 l_ptr->reset_checkpoint = l_ptr->next_in_no;
454 l_ptr->exp_msg_count = 0;
Per Liden4323add2006-01-18 00:38:21 +0100455 tipc_link_reset_fragments(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100456 }
457
458 /* Notify subscribers */
459 list_for_each_entry_safe(ns, tns, &n_ptr->nsub, nodesub_list) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900460 ns->node = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100461 list_del_init(&ns->nodesub_list);
Per Liden4323add2006-01-18 00:38:21 +0100462 tipc_k_signal((Handler)ns->handle_node_down,
463 (unsigned long)ns->usr_handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100464 }
Allan Stephens5a68d5ee2010-08-17 11:00:16 +0000465
466 /* Prevent re-contact with node until all cleanup is done */
467
468 n_ptr->cleanup_required = 1;
469 tipc_k_signal((Handler)node_cleanup_finished, n_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100470}
471
472/**
Per Liden4323add2006-01-18 00:38:21 +0100473 * tipc_node_select_next_hop - find the next-hop node for a message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900474 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100475 * Called by when cluster local lookup has failed.
476 */
477
David S. Miller6c000552008-09-02 23:38:32 -0700478struct tipc_node *tipc_node_select_next_hop(u32 addr, u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100479{
David S. Miller6c000552008-09-02 23:38:32 -0700480 struct tipc_node *n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100481 u32 router_addr;
482
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900483 if (!tipc_addr_domain_valid(addr))
484 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100485
486 /* Look for direct link to destination processsor */
Per Liden4323add2006-01-18 00:38:21 +0100487 n_ptr = tipc_node_find(addr);
488 if (n_ptr && tipc_node_has_active_links(n_ptr))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900489 return n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100490
491 /* Cluster local system nodes *must* have direct links */
492 if (!is_slave(addr) && in_own_cluster(addr))
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800493 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100494
495 /* Look for cluster local router with direct link to node */
Per Liden4323add2006-01-18 00:38:21 +0100496 router_addr = tipc_node_select_router(n_ptr, selector);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900497 if (router_addr)
498 return tipc_node_select(router_addr, selector);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100499
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900500 /* Slave nodes can only be accessed within own cluster via a
Per Lidenb97bf3f2006-01-02 19:04:38 +0100501 known router with direct link -- if no router was found,give up */
502 if (is_slave(addr))
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800503 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100504
505 /* Inter zone/cluster -- find any direct link to remote cluster */
506 addr = tipc_addr(tipc_zone(addr), tipc_cluster(addr), 0);
Per Liden4323add2006-01-18 00:38:21 +0100507 n_ptr = tipc_net_select_remote_node(addr, selector);
508 if (n_ptr && tipc_node_has_active_links(n_ptr))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900509 return n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100510
511 /* Last resort -- look for any router to anywhere in remote zone */
Per Liden4323add2006-01-18 00:38:21 +0100512 router_addr = tipc_net_select_router(addr, selector);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900513 if (router_addr)
514 return tipc_node_select(router_addr, selector);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100515
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900516 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100517}
518
519/**
Per Liden4323add2006-01-18 00:38:21 +0100520 * tipc_node_select_router - select router to reach specified node
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900521 *
522 * Uses a deterministic and fair algorithm for selecting router node.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100523 */
524
David S. Miller6c000552008-09-02 23:38:32 -0700525u32 tipc_node_select_router(struct tipc_node *n_ptr, u32 ref)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100526{
527 u32 ulim;
528 u32 mask;
529 u32 start;
530 u32 r;
531
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900532 if (!n_ptr)
533 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100534
535 if (n_ptr->last_router < 0)
536 return 0;
537 ulim = ((n_ptr->last_router + 1) * 32) - 1;
538
539 /* Start entry must be random */
540 mask = tipc_max_nodes;
541 while (mask > ulim)
542 mask >>= 1;
543 start = ref & mask;
544 r = start;
545
546 /* Lookup upwards with wrap-around */
547 do {
548 if (((n_ptr->routers[r / 32]) >> (r % 32)) & 1)
549 break;
550 } while (++r <= ulim);
551 if (r > ulim) {
552 r = 1;
553 do {
554 if (((n_ptr->routers[r / 32]) >> (r % 32)) & 1)
555 break;
556 } while (++r < start);
557 assert(r != start);
558 }
559 assert(r && (r <= ulim));
560 return tipc_addr(own_zone(), own_cluster(), r);
561}
562
David S. Miller6c000552008-09-02 23:38:32 -0700563void tipc_node_add_router(struct tipc_node *n_ptr, u32 router)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100564{
565 u32 r_num = tipc_node(router);
566
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900567 n_ptr->routers[r_num / 32] =
Per Lidenb97bf3f2006-01-02 19:04:38 +0100568 ((1 << (r_num % 32)) | n_ptr->routers[r_num / 32]);
569 n_ptr->last_router = tipc_max_nodes / 32;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900570 while ((--n_ptr->last_router >= 0) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +0100571 !n_ptr->routers[n_ptr->last_router]);
572}
573
David S. Miller6c000552008-09-02 23:38:32 -0700574void tipc_node_remove_router(struct tipc_node *n_ptr, u32 router)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100575{
576 u32 r_num = tipc_node(router);
577
578 if (n_ptr->last_router < 0)
579 return; /* No routes */
580
581 n_ptr->routers[r_num / 32] =
582 ((~(1 << (r_num % 32))) & (n_ptr->routers[r_num / 32]));
583 n_ptr->last_router = tipc_max_nodes / 32;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900584 while ((--n_ptr->last_router >= 0) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +0100585 !n_ptr->routers[n_ptr->last_router]);
586
Per Liden4323add2006-01-18 00:38:21 +0100587 if (!tipc_node_is_up(n_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100588 node_lost_contact(n_ptr);
589}
590
Per Liden4323add2006-01-18 00:38:21 +0100591struct sk_buff *tipc_node_get_nodes(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100592{
593 u32 domain;
594 struct sk_buff *buf;
David S. Miller6c000552008-09-02 23:38:32 -0700595 struct tipc_node *n_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900596 struct tipc_node_info node_info;
Allan Stephensea138472006-06-29 12:33:20 -0700597 u32 payload_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100598
599 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NET_ADDR))
Per Liden4323add2006-01-18 00:38:21 +0100600 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100601
Al Viro3e6c8cd2006-11-08 00:19:09 -0800602 domain = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
Per Liden4323add2006-01-18 00:38:21 +0100603 if (!tipc_addr_domain_valid(domain))
604 return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
605 " (network address)");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100606
Allan Stephens1aad72d2008-07-14 22:44:58 -0700607 read_lock_bh(&tipc_net_lock);
608 if (!tipc_nodes) {
609 read_unlock_bh(&tipc_net_lock);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900610 return tipc_cfg_reply_none();
Allan Stephens1aad72d2008-07-14 22:44:58 -0700611 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100612
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900613 /* For now, get space for all other nodes
Per Lidenb97bf3f2006-01-02 19:04:38 +0100614 (will need to modify this when slave nodes are supported */
615
Allan Stephensea138472006-06-29 12:33:20 -0700616 payload_size = TLV_SPACE(sizeof(node_info)) * (tipc_max_nodes - 1);
Allan Stephens1aad72d2008-07-14 22:44:58 -0700617 if (payload_size > 32768u) {
618 read_unlock_bh(&tipc_net_lock);
Allan Stephensea138472006-06-29 12:33:20 -0700619 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
620 " (too many nodes)");
Allan Stephens1aad72d2008-07-14 22:44:58 -0700621 }
Allan Stephensea138472006-06-29 12:33:20 -0700622 buf = tipc_cfg_reply_alloc(payload_size);
Allan Stephens1aad72d2008-07-14 22:44:58 -0700623 if (!buf) {
624 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100625 return NULL;
Allan Stephens1aad72d2008-07-14 22:44:58 -0700626 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100627
628 /* Add TLVs for all nodes in scope */
629
Per Liden4323add2006-01-18 00:38:21 +0100630 for (n_ptr = tipc_nodes; n_ptr; n_ptr = n_ptr->next) {
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000631 if (!tipc_in_scope(domain, n_ptr->addr))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100632 continue;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900633 node_info.addr = htonl(n_ptr->addr);
634 node_info.up = htonl(tipc_node_is_up(n_ptr));
635 tipc_cfg_append_tlv(buf, TIPC_TLV_NODE_INFO,
Per Liden4323add2006-01-18 00:38:21 +0100636 &node_info, sizeof(node_info));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100637 }
638
Allan Stephens1aad72d2008-07-14 22:44:58 -0700639 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100640 return buf;
641}
642
Per Liden4323add2006-01-18 00:38:21 +0100643struct sk_buff *tipc_node_get_links(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100644{
645 u32 domain;
646 struct sk_buff *buf;
David S. Miller6c000552008-09-02 23:38:32 -0700647 struct tipc_node *n_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900648 struct tipc_link_info link_info;
Allan Stephensea138472006-06-29 12:33:20 -0700649 u32 payload_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100650
651 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NET_ADDR))
Per Liden4323add2006-01-18 00:38:21 +0100652 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100653
Al Viro3e6c8cd2006-11-08 00:19:09 -0800654 domain = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
Per Liden4323add2006-01-18 00:38:21 +0100655 if (!tipc_addr_domain_valid(domain))
656 return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
657 " (network address)");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100658
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900659 if (tipc_mode != TIPC_NET_MODE)
660 return tipc_cfg_reply_none();
661
Allan Stephens1aad72d2008-07-14 22:44:58 -0700662 read_lock_bh(&tipc_net_lock);
663
Allan Stephensea138472006-06-29 12:33:20 -0700664 /* Get space for all unicast links + multicast link */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100665
Allan Stephensea138472006-06-29 12:33:20 -0700666 payload_size = TLV_SPACE(sizeof(link_info)) *
667 (tipc_net.zones[tipc_zone(tipc_own_addr)]->links + 1);
Allan Stephens1aad72d2008-07-14 22:44:58 -0700668 if (payload_size > 32768u) {
669 read_unlock_bh(&tipc_net_lock);
Allan Stephensea138472006-06-29 12:33:20 -0700670 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
671 " (too many links)");
Allan Stephens1aad72d2008-07-14 22:44:58 -0700672 }
Allan Stephensea138472006-06-29 12:33:20 -0700673 buf = tipc_cfg_reply_alloc(payload_size);
Allan Stephens1aad72d2008-07-14 22:44:58 -0700674 if (!buf) {
675 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100676 return NULL;
Allan Stephens1aad72d2008-07-14 22:44:58 -0700677 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100678
679 /* Add TLV for broadcast link */
680
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900681 link_info.dest = htonl(tipc_own_addr & 0xfffff00);
682 link_info.up = htonl(1);
Stephen Hemminger4b704d52009-03-18 19:11:29 -0700683 strlcpy(link_info.str, tipc_bclink_name, TIPC_MAX_LINK_NAME);
Per Liden4323add2006-01-18 00:38:21 +0100684 tipc_cfg_append_tlv(buf, TIPC_TLV_LINK_INFO, &link_info, sizeof(link_info));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100685
686 /* Add TLVs for any other links in scope */
687
Per Liden4323add2006-01-18 00:38:21 +0100688 for (n_ptr = tipc_nodes; n_ptr; n_ptr = n_ptr->next) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900689 u32 i;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100690
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000691 if (!tipc_in_scope(domain, n_ptr->addr))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100692 continue;
Allan Stephens1aad72d2008-07-14 22:44:58 -0700693 tipc_node_lock(n_ptr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900694 for (i = 0; i < MAX_BEARERS; i++) {
695 if (!n_ptr->links[i])
696 continue;
697 link_info.dest = htonl(n_ptr->addr);
698 link_info.up = htonl(tipc_link_is_up(n_ptr->links[i]));
699 strcpy(link_info.str, n_ptr->links[i]->name);
700 tipc_cfg_append_tlv(buf, TIPC_TLV_LINK_INFO,
Per Liden4323add2006-01-18 00:38:21 +0100701 &link_info, sizeof(link_info));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900702 }
Allan Stephens1aad72d2008-07-14 22:44:58 -0700703 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100704 }
705
Allan Stephens1aad72d2008-07-14 22:44:58 -0700706 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100707 return buf;
708}