blob: 823e9abb7ef5af39d6194b682f3d3ffc4bfb6f3c [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
David S. Miller6c000552008-09-02 23:38:32 -070053struct tipc_node *tipc_nodes = NULL; /* sorted list of nodes within cluster */
Per Lidenb97bf3f2006-01-02 19:04:38 +010054
Allan Stephens2ecb0922008-05-21 14:53:00 -070055static DEFINE_SPINLOCK(node_create_lock);
56
Per Lidenb97bf3f2006-01-02 19:04:38 +010057u32 tipc_own_tag = 0;
58
Allan Stephens2ecb0922008-05-21 14:53:00 -070059/**
60 * tipc_node_create - create neighboring node
61 *
62 * Currently, this routine is called by neighbor discovery code, which holds
63 * net_lock for reading only. We must take node_create_lock to ensure a node
64 * isn't created twice if two different bearers discover the node at the same
65 * time. (It would be preferable to switch to holding net_lock in write mode,
66 * but this is a non-trivial change.)
67 */
68
David S. Miller6c000552008-09-02 23:38:32 -070069struct tipc_node *tipc_node_create(u32 addr)
Per Lidenb97bf3f2006-01-02 19:04:38 +010070{
71 struct cluster *c_ptr;
David S. Miller6c000552008-09-02 23:38:32 -070072 struct tipc_node *n_ptr;
73 struct tipc_node **curr_node;
Per Lidenb97bf3f2006-01-02 19:04:38 +010074
Allan Stephens2ecb0922008-05-21 14:53:00 -070075 spin_lock_bh(&node_create_lock);
76
77 for (n_ptr = tipc_nodes; n_ptr; n_ptr = n_ptr->next) {
78 if (addr < n_ptr->addr)
79 break;
80 if (addr == n_ptr->addr) {
81 spin_unlock_bh(&node_create_lock);
82 return n_ptr;
83 }
84 }
85
Arnaldo Carvalho de Melo2710b572006-11-21 01:22:12 -020086 n_ptr = kzalloc(sizeof(*n_ptr),GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -070087 if (!n_ptr) {
Allan Stephens2ecb0922008-05-21 14:53:00 -070088 spin_unlock_bh(&node_create_lock);
Allan Stephensa10bd922006-06-25 23:52:17 -070089 warn("Node creation failed, no memory\n");
90 return NULL;
91 }
Per Lidenb97bf3f2006-01-02 19:04:38 +010092
Allan Stephensa10bd922006-06-25 23:52:17 -070093 c_ptr = tipc_cltr_find(addr);
94 if (!c_ptr) {
95 c_ptr = tipc_cltr_create(addr);
96 }
97 if (!c_ptr) {
Allan Stephens2ecb0922008-05-21 14:53:00 -070098 spin_unlock_bh(&node_create_lock);
Allan Stephensa10bd922006-06-25 23:52:17 -070099 kfree(n_ptr);
100 return NULL;
101 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900102
Allan Stephensa10bd922006-06-25 23:52:17 -0700103 n_ptr->addr = addr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900104 spin_lock_init(&n_ptr->lock);
Allan Stephensa10bd922006-06-25 23:52:17 -0700105 INIT_LIST_HEAD(&n_ptr->nsub);
106 n_ptr->owner = c_ptr;
107 tipc_cltr_attach_node(c_ptr, n_ptr);
108 n_ptr->last_router = -1;
109
110 /* Insert node into ordered list */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900111 for (curr_node = &tipc_nodes; *curr_node;
Allan Stephensa10bd922006-06-25 23:52:17 -0700112 curr_node = &(*curr_node)->next) {
113 if (addr < (*curr_node)->addr) {
114 n_ptr->next = *curr_node;
115 break;
116 }
117 }
118 (*curr_node) = n_ptr;
Allan Stephens2ecb0922008-05-21 14:53:00 -0700119 spin_unlock_bh(&node_create_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100120 return n_ptr;
121}
122
David S. Miller6c000552008-09-02 23:38:32 -0700123void tipc_node_delete(struct tipc_node *n_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100124{
125 if (!n_ptr)
126 return;
127
Per Lidenb97bf3f2006-01-02 19:04:38 +0100128 dbg("node %x deleted\n", n_ptr->addr);
129 kfree(n_ptr);
130}
131
132
133/**
Per Liden4323add2006-01-18 00:38:21 +0100134 * tipc_node_link_up - handle addition of link
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900135 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100136 * Link becomes active (alone or shared) or standby, depending on its priority.
137 */
138
David S. Miller6c000552008-09-02 23:38:32 -0700139void tipc_node_link_up(struct tipc_node *n_ptr, struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100140{
141 struct link **active = &n_ptr->active_links[0];
142
Allan Stephens5392d642006-06-25 23:52:50 -0700143 n_ptr->working_links++;
144
Per Lidenb97bf3f2006-01-02 19:04:38 +0100145 info("Established link <%s> on network plane %c\n",
146 l_ptr->name, l_ptr->b_ptr->net_plane);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900147
Per Lidenb97bf3f2006-01-02 19:04:38 +0100148 if (!active[0]) {
149 dbg(" link %x into %x/%x\n", l_ptr, &active[0], &active[1]);
150 active[0] = active[1] = l_ptr;
151 node_established_contact(n_ptr);
152 return;
153 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900154 if (l_ptr->priority < active[0]->priority) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700155 info("New link <%s> becomes standby\n", l_ptr->name);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100156 return;
157 }
Per Liden4323add2006-01-18 00:38:21 +0100158 tipc_link_send_duplicate(active[0], l_ptr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900159 if (l_ptr->priority == active[0]->priority) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100160 active[0] = l_ptr;
161 return;
162 }
Allan Stephensa10bd922006-06-25 23:52:17 -0700163 info("Old link <%s> becomes standby\n", active[0]->name);
164 if (active[1] != active[0])
165 info("Old link <%s> becomes standby\n", active[1]->name);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100166 active[0] = active[1] = l_ptr;
167}
168
169/**
170 * node_select_active_links - select active link
171 */
172
David S. Miller6c000552008-09-02 23:38:32 -0700173static void node_select_active_links(struct tipc_node *n_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100174{
175 struct link **active = &n_ptr->active_links[0];
176 u32 i;
177 u32 highest_prio = 0;
178
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900179 active[0] = active[1] = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100180
181 for (i = 0; i < MAX_BEARERS; i++) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900182 struct link *l_ptr = n_ptr->links[i];
Per Lidenb97bf3f2006-01-02 19:04:38 +0100183
Per Liden4323add2006-01-18 00:38:21 +0100184 if (!l_ptr || !tipc_link_is_up(l_ptr) ||
Per Lidenb97bf3f2006-01-02 19:04:38 +0100185 (l_ptr->priority < highest_prio))
186 continue;
187
188 if (l_ptr->priority > highest_prio) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900189 highest_prio = l_ptr->priority;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100190 active[0] = active[1] = l_ptr;
191 } else {
192 active[1] = l_ptr;
193 }
194 }
195}
196
197/**
Per Liden4323add2006-01-18 00:38:21 +0100198 * tipc_node_link_down - handle loss of link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100199 */
200
David S. Miller6c000552008-09-02 23:38:32 -0700201void tipc_node_link_down(struct tipc_node *n_ptr, struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100202{
203 struct link **active;
204
Allan Stephens5392d642006-06-25 23:52:50 -0700205 n_ptr->working_links--;
206
Per Liden4323add2006-01-18 00:38:21 +0100207 if (!tipc_link_is_active(l_ptr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100208 info("Lost standby link <%s> on network plane %c\n",
209 l_ptr->name, l_ptr->b_ptr->net_plane);
210 return;
211 }
212 info("Lost link <%s> on network plane %c\n",
213 l_ptr->name, l_ptr->b_ptr->net_plane);
214
215 active = &n_ptr->active_links[0];
216 if (active[0] == l_ptr)
217 active[0] = active[1];
218 if (active[1] == l_ptr)
219 active[1] = active[0];
220 if (active[0] == l_ptr)
221 node_select_active_links(n_ptr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900222 if (tipc_node_is_up(n_ptr))
Per Liden4323add2006-01-18 00:38:21 +0100223 tipc_link_changeover(l_ptr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900224 else
Per Lidenb97bf3f2006-01-02 19:04:38 +0100225 node_lost_contact(n_ptr);
226}
227
David S. Miller6c000552008-09-02 23:38:32 -0700228int tipc_node_has_active_links(struct tipc_node *n_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100229{
Allan Stephens76ae0d72010-08-17 11:00:12 +0000230 return n_ptr->active_links[0] != NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100231}
232
David S. Miller6c000552008-09-02 23:38:32 -0700233int tipc_node_has_redundant_links(struct tipc_node *n_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100234{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000235 return n_ptr->working_links > 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100236}
237
David S. Miller6c000552008-09-02 23:38:32 -0700238static int tipc_node_has_active_routes(struct tipc_node *n_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100239{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000240 return n_ptr && (n_ptr->last_router >= 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100241}
242
David S. Miller6c000552008-09-02 23:38:32 -0700243int tipc_node_is_up(struct tipc_node *n_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100244{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000245 return tipc_node_has_active_links(n_ptr) || tipc_node_has_active_routes(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100246}
247
David S. Miller6c000552008-09-02 23:38:32 -0700248struct tipc_node *tipc_node_attach_link(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100249{
David S. Miller6c000552008-09-02 23:38:32 -0700250 struct tipc_node *n_ptr = tipc_node_find(l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100251
252 if (!n_ptr)
Per Liden4323add2006-01-18 00:38:21 +0100253 n_ptr = tipc_node_create(l_ptr->addr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900254 if (n_ptr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100255 u32 bearer_id = l_ptr->b_ptr->identity;
256 char addr_string[16];
257
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900258 if (n_ptr->link_cnt >= 2) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900259 err("Attempt to create third link to %s\n",
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000260 tipc_addr_string_fill(addr_string, n_ptr->addr));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900261 return NULL;
262 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100263
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900264 if (!n_ptr->links[bearer_id]) {
265 n_ptr->links[bearer_id] = l_ptr;
266 tipc_net.zones[tipc_zone(l_ptr->addr)]->links++;
267 n_ptr->link_cnt++;
268 return n_ptr;
269 }
Frans Popa570f092010-03-24 07:57:29 +0000270 err("Attempt to establish second link on <%s> to %s\n",
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900271 l_ptr->b_ptr->publ.name,
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000272 tipc_addr_string_fill(addr_string, l_ptr->addr));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900273 }
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800274 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100275}
276
David S. Miller6c000552008-09-02 23:38:32 -0700277void tipc_node_detach_link(struct tipc_node *n_ptr, struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100278{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800279 n_ptr->links[l_ptr->b_ptr->identity] = NULL;
Per Liden4323add2006-01-18 00:38:21 +0100280 tipc_net.zones[tipc_zone(l_ptr->addr)]->links--;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100281 n_ptr->link_cnt--;
282}
283
284/*
285 * Routing table management - five cases to handle:
286 *
287 * 1: A link towards a zone/cluster external 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())
292 *
293 * 2: A link towards a slave node comes up.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900294 * => Send a multicast message updating routing tables of all
295 * system nodes within own cluster that the new destination
296 * can be reached via this node.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100297 * (node.establishedContact()=>cluster.multicastNewRoute())
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900298 * => Send a message to the slave node about existence
Per Lidenb97bf3f2006-01-02 19:04:38 +0100299 * of all system nodes within cluster:
300 * (node.establishedContact()=>cluster.sendLocalRoutes())
301 *
302 * 3: A new cluster local system node becomes available.
303 * => Send message(s) to this particular node containing
304 * information about all cluster external and slave
305 * nodes which can be reached via this node.
306 * (node.establishedContact()==>network.sendExternalRoutes())
307 * (node.establishedContact()==>network.sendSlaveRoutes())
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900308 * => Send messages to all directly connected slave nodes
Per Lidenb97bf3f2006-01-02 19:04:38 +0100309 * containing information about the existence of the new node
310 * (node.establishedContact()=>cluster.multicastNewRoute())
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900311 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100312 * 4: The link towards a zone/cluster external node or slave
313 * node goes down.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900314 * => Send a multcast message updating routing tables of all
Per Lidenb97bf3f2006-01-02 19:04:38 +0100315 * nodes within cluster that the new destination can not any
316 * longer be reached via this node.
317 * (node.lostAllLinks()=>cluster.bcastLostRoute())
318 *
319 * 5: A cluster local system node becomes unavailable.
320 * => Remove all references to this node from the local
321 * routing tables. Note: This is a completely node
322 * local operation.
323 * (node.lostAllLinks()=>network.removeAsRouter())
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900324 * => Send messages to all directly connected slave nodes
Per Lidenb97bf3f2006-01-02 19:04:38 +0100325 * containing information about loss of the node
326 * (node.establishedContact()=>cluster.multicastLostRoute())
327 *
328 */
329
David S. Miller6c000552008-09-02 23:38:32 -0700330static void node_established_contact(struct tipc_node *n_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100331{
332 struct cluster *c_ptr;
333
334 dbg("node_established_contact:-> %x\n", n_ptr->addr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900335 if (!tipc_node_has_active_routes(n_ptr) && in_own_cluster(n_ptr->addr)) {
Per Liden4323add2006-01-18 00:38:21 +0100336 tipc_k_signal((Handler)tipc_named_node_up, n_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100337 }
338
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900339 /* Syncronize broadcast acks */
340 n_ptr->bclink.acked = tipc_bclink_get_last_sent();
Per Lidenb97bf3f2006-01-02 19:04:38 +0100341
342 if (is_slave(tipc_own_addr))
343 return;
344 if (!in_own_cluster(n_ptr->addr)) {
345 /* Usage case 1 (see above) */
Per Liden4323add2006-01-18 00:38:21 +0100346 c_ptr = tipc_cltr_find(tipc_own_addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100347 if (!c_ptr)
Per Liden4323add2006-01-18 00:38:21 +0100348 c_ptr = tipc_cltr_create(tipc_own_addr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900349 if (c_ptr)
350 tipc_cltr_bcast_new_route(c_ptr, n_ptr->addr, 1,
Per Liden4323add2006-01-18 00:38:21 +0100351 tipc_max_nodes);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100352 return;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900353 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100354
355 c_ptr = n_ptr->owner;
356 if (is_slave(n_ptr->addr)) {
357 /* Usage case 2 (see above) */
Per Liden4323add2006-01-18 00:38:21 +0100358 tipc_cltr_bcast_new_route(c_ptr, n_ptr->addr, 1, tipc_max_nodes);
359 tipc_cltr_send_local_routes(c_ptr, n_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100360 return;
361 }
362
363 if (n_ptr->bclink.supported) {
Per Liden4323add2006-01-18 00:38:21 +0100364 tipc_nmap_add(&tipc_cltr_bcast_nodes, n_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100365 if (n_ptr->addr < tipc_own_addr)
366 tipc_own_tag++;
367 }
368
369 /* Case 3 (see above) */
Per Liden4323add2006-01-18 00:38:21 +0100370 tipc_net_send_external_routes(n_ptr->addr);
371 tipc_cltr_send_slave_routes(c_ptr, n_ptr->addr);
372 tipc_cltr_bcast_new_route(c_ptr, n_ptr->addr, LOWEST_SLAVE,
373 tipc_highest_allowed_slave);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100374}
375
Allan Stephens5a68d5ee2010-08-17 11:00:16 +0000376static void node_cleanup_finished(unsigned long node_addr)
377{
378 struct tipc_node *n_ptr;
379
380 read_lock_bh(&tipc_net_lock);
381 n_ptr = tipc_node_find(node_addr);
382 if (n_ptr) {
383 tipc_node_lock(n_ptr);
384 n_ptr->cleanup_required = 0;
385 tipc_node_unlock(n_ptr);
386 }
387 read_unlock_bh(&tipc_net_lock);
388}
389
David S. Miller6c000552008-09-02 23:38:32 -0700390static void node_lost_contact(struct tipc_node *n_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100391{
392 struct cluster *c_ptr;
David S. Miller6c000552008-09-02 23:38:32 -0700393 struct tipc_node_subscr *ns, *tns;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100394 char addr_string[16];
395 u32 i;
396
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900397 /* Clean up broadcast reception remains */
398 n_ptr->bclink.gap_after = n_ptr->bclink.gap_to = 0;
399 while (n_ptr->bclink.deferred_head) {
400 struct sk_buff* buf = n_ptr->bclink.deferred_head;
401 n_ptr->bclink.deferred_head = buf->next;
402 buf_discard(buf);
403 }
404 if (n_ptr->bclink.defragm) {
405 buf_discard(n_ptr->bclink.defragm);
406 n_ptr->bclink.defragm = NULL;
407 }
408 if (in_own_cluster(n_ptr->addr) && n_ptr->bclink.supported) {
409 tipc_bclink_acknowledge(n_ptr, mod(n_ptr->bclink.acked + 10000));
410 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100411
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900412 /* Update routing tables */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100413 if (is_slave(tipc_own_addr)) {
Per Liden4323add2006-01-18 00:38:21 +0100414 tipc_net_remove_as_router(n_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100415 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900416 if (!in_own_cluster(n_ptr->addr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100417 /* Case 4 (see above) */
Per Liden4323add2006-01-18 00:38:21 +0100418 c_ptr = tipc_cltr_find(tipc_own_addr);
419 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 /* Case 5 (see above) */
Per Liden4323add2006-01-18 00:38:21 +0100423 c_ptr = tipc_cltr_find(n_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100424 if (is_slave(n_ptr->addr)) {
Per Liden4323add2006-01-18 00:38:21 +0100425 tipc_cltr_bcast_lost_route(c_ptr, n_ptr->addr, 1,
426 tipc_max_nodes);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100427 } else {
428 if (n_ptr->bclink.supported) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900429 tipc_nmap_remove(&tipc_cltr_bcast_nodes,
Per Liden4323add2006-01-18 00:38:21 +0100430 n_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100431 if (n_ptr->addr < tipc_own_addr)
432 tipc_own_tag--;
433 }
Per Liden4323add2006-01-18 00:38:21 +0100434 tipc_net_remove_as_router(n_ptr->addr);
435 tipc_cltr_bcast_lost_route(c_ptr, n_ptr->addr,
436 LOWEST_SLAVE,
437 tipc_highest_allowed_slave);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100438 }
439 }
440 }
Per Liden4323add2006-01-18 00:38:21 +0100441 if (tipc_node_has_active_routes(n_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100442 return;
443
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900444 info("Lost contact with %s\n",
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000445 tipc_addr_string_fill(addr_string, n_ptr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100446
447 /* Abort link changeover */
448 for (i = 0; i < MAX_BEARERS; i++) {
449 struct link *l_ptr = n_ptr->links[i];
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900450 if (!l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100451 continue;
452 l_ptr->reset_checkpoint = l_ptr->next_in_no;
453 l_ptr->exp_msg_count = 0;
Per Liden4323add2006-01-18 00:38:21 +0100454 tipc_link_reset_fragments(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100455 }
456
457 /* Notify subscribers */
458 list_for_each_entry_safe(ns, tns, &n_ptr->nsub, nodesub_list) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900459 ns->node = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100460 list_del_init(&ns->nodesub_list);
Per Liden4323add2006-01-18 00:38:21 +0100461 tipc_k_signal((Handler)ns->handle_node_down,
462 (unsigned long)ns->usr_handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100463 }
Allan Stephens5a68d5ee2010-08-17 11:00:16 +0000464
465 /* Prevent re-contact with node until all cleanup is done */
466
467 n_ptr->cleanup_required = 1;
468 tipc_k_signal((Handler)node_cleanup_finished, n_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100469}
470
471/**
Per Liden4323add2006-01-18 00:38:21 +0100472 * tipc_node_select_next_hop - find the next-hop node for a message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900473 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100474 * Called by when cluster local lookup has failed.
475 */
476
David S. Miller6c000552008-09-02 23:38:32 -0700477struct tipc_node *tipc_node_select_next_hop(u32 addr, u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100478{
David S. Miller6c000552008-09-02 23:38:32 -0700479 struct tipc_node *n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100480 u32 router_addr;
481
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900482 if (!tipc_addr_domain_valid(addr))
483 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100484
485 /* Look for direct link to destination processsor */
Per Liden4323add2006-01-18 00:38:21 +0100486 n_ptr = tipc_node_find(addr);
487 if (n_ptr && tipc_node_has_active_links(n_ptr))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900488 return n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100489
490 /* Cluster local system nodes *must* have direct links */
491 if (!is_slave(addr) && in_own_cluster(addr))
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800492 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100493
494 /* Look for cluster local router with direct link to node */
Per Liden4323add2006-01-18 00:38:21 +0100495 router_addr = tipc_node_select_router(n_ptr, selector);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900496 if (router_addr)
497 return tipc_node_select(router_addr, selector);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100498
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900499 /* Slave nodes can only be accessed within own cluster via a
Per Lidenb97bf3f2006-01-02 19:04:38 +0100500 known router with direct link -- if no router was found,give up */
501 if (is_slave(addr))
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800502 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100503
504 /* Inter zone/cluster -- find any direct link to remote cluster */
505 addr = tipc_addr(tipc_zone(addr), tipc_cluster(addr), 0);
Per Liden4323add2006-01-18 00:38:21 +0100506 n_ptr = tipc_net_select_remote_node(addr, selector);
507 if (n_ptr && tipc_node_has_active_links(n_ptr))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900508 return n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100509
510 /* Last resort -- look for any router to anywhere in remote zone */
Per Liden4323add2006-01-18 00:38:21 +0100511 router_addr = tipc_net_select_router(addr, selector);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900512 if (router_addr)
513 return tipc_node_select(router_addr, selector);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100514
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900515 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100516}
517
518/**
Per Liden4323add2006-01-18 00:38:21 +0100519 * tipc_node_select_router - select router to reach specified node
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900520 *
521 * Uses a deterministic and fair algorithm for selecting router node.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100522 */
523
David S. Miller6c000552008-09-02 23:38:32 -0700524u32 tipc_node_select_router(struct tipc_node *n_ptr, u32 ref)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100525{
526 u32 ulim;
527 u32 mask;
528 u32 start;
529 u32 r;
530
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900531 if (!n_ptr)
532 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100533
534 if (n_ptr->last_router < 0)
535 return 0;
536 ulim = ((n_ptr->last_router + 1) * 32) - 1;
537
538 /* Start entry must be random */
539 mask = tipc_max_nodes;
540 while (mask > ulim)
541 mask >>= 1;
542 start = ref & mask;
543 r = start;
544
545 /* Lookup upwards with wrap-around */
546 do {
547 if (((n_ptr->routers[r / 32]) >> (r % 32)) & 1)
548 break;
549 } while (++r <= ulim);
550 if (r > ulim) {
551 r = 1;
552 do {
553 if (((n_ptr->routers[r / 32]) >> (r % 32)) & 1)
554 break;
555 } while (++r < start);
556 assert(r != start);
557 }
558 assert(r && (r <= ulim));
559 return tipc_addr(own_zone(), own_cluster(), r);
560}
561
David S. Miller6c000552008-09-02 23:38:32 -0700562void tipc_node_add_router(struct tipc_node *n_ptr, u32 router)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100563{
564 u32 r_num = tipc_node(router);
565
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900566 n_ptr->routers[r_num / 32] =
Per Lidenb97bf3f2006-01-02 19:04:38 +0100567 ((1 << (r_num % 32)) | n_ptr->routers[r_num / 32]);
568 n_ptr->last_router = tipc_max_nodes / 32;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900569 while ((--n_ptr->last_router >= 0) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +0100570 !n_ptr->routers[n_ptr->last_router]);
571}
572
David S. Miller6c000552008-09-02 23:38:32 -0700573void tipc_node_remove_router(struct tipc_node *n_ptr, u32 router)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100574{
575 u32 r_num = tipc_node(router);
576
577 if (n_ptr->last_router < 0)
578 return; /* No routes */
579
580 n_ptr->routers[r_num / 32] =
581 ((~(1 << (r_num % 32))) & (n_ptr->routers[r_num / 32]));
582 n_ptr->last_router = tipc_max_nodes / 32;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900583 while ((--n_ptr->last_router >= 0) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +0100584 !n_ptr->routers[n_ptr->last_router]);
585
Per Liden4323add2006-01-18 00:38:21 +0100586 if (!tipc_node_is_up(n_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100587 node_lost_contact(n_ptr);
588}
589
Per Lidenb97bf3f2006-01-02 19:04:38 +0100590u32 tipc_available_nodes(const u32 domain)
591{
David S. Miller6c000552008-09-02 23:38:32 -0700592 struct tipc_node *n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100593 u32 cnt = 0;
594
Allan Stephens1aad72d2008-07-14 22:44:58 -0700595 read_lock_bh(&tipc_net_lock);
Per Liden4323add2006-01-18 00:38:21 +0100596 for (n_ptr = tipc_nodes; n_ptr; n_ptr = n_ptr->next) {
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000597 if (!tipc_in_scope(domain, n_ptr->addr))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100598 continue;
Per Liden4323add2006-01-18 00:38:21 +0100599 if (tipc_node_is_up(n_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100600 cnt++;
601 }
Allan Stephens1aad72d2008-07-14 22:44:58 -0700602 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100603 return cnt;
604}
605
Per Liden4323add2006-01-18 00:38:21 +0100606struct sk_buff *tipc_node_get_nodes(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100607{
608 u32 domain;
609 struct sk_buff *buf;
David S. Miller6c000552008-09-02 23:38:32 -0700610 struct tipc_node *n_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900611 struct tipc_node_info node_info;
Allan Stephensea138472006-06-29 12:33:20 -0700612 u32 payload_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100613
614 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NET_ADDR))
Per Liden4323add2006-01-18 00:38:21 +0100615 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100616
Al Viro3e6c8cd2006-11-08 00:19:09 -0800617 domain = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
Per Liden4323add2006-01-18 00:38:21 +0100618 if (!tipc_addr_domain_valid(domain))
619 return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
620 " (network address)");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100621
Allan Stephens1aad72d2008-07-14 22:44:58 -0700622 read_lock_bh(&tipc_net_lock);
623 if (!tipc_nodes) {
624 read_unlock_bh(&tipc_net_lock);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900625 return tipc_cfg_reply_none();
Allan Stephens1aad72d2008-07-14 22:44:58 -0700626 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100627
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900628 /* For now, get space for all other nodes
Per Lidenb97bf3f2006-01-02 19:04:38 +0100629 (will need to modify this when slave nodes are supported */
630
Allan Stephensea138472006-06-29 12:33:20 -0700631 payload_size = TLV_SPACE(sizeof(node_info)) * (tipc_max_nodes - 1);
Allan Stephens1aad72d2008-07-14 22:44:58 -0700632 if (payload_size > 32768u) {
633 read_unlock_bh(&tipc_net_lock);
Allan Stephensea138472006-06-29 12:33:20 -0700634 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
635 " (too many nodes)");
Allan Stephens1aad72d2008-07-14 22:44:58 -0700636 }
Allan Stephensea138472006-06-29 12:33:20 -0700637 buf = tipc_cfg_reply_alloc(payload_size);
Allan Stephens1aad72d2008-07-14 22:44:58 -0700638 if (!buf) {
639 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100640 return NULL;
Allan Stephens1aad72d2008-07-14 22:44:58 -0700641 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100642
643 /* Add TLVs for all nodes in scope */
644
Per Liden4323add2006-01-18 00:38:21 +0100645 for (n_ptr = tipc_nodes; n_ptr; n_ptr = n_ptr->next) {
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000646 if (!tipc_in_scope(domain, n_ptr->addr))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100647 continue;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900648 node_info.addr = htonl(n_ptr->addr);
649 node_info.up = htonl(tipc_node_is_up(n_ptr));
650 tipc_cfg_append_tlv(buf, TIPC_TLV_NODE_INFO,
Per Liden4323add2006-01-18 00:38:21 +0100651 &node_info, sizeof(node_info));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100652 }
653
Allan Stephens1aad72d2008-07-14 22:44:58 -0700654 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100655 return buf;
656}
657
Per Liden4323add2006-01-18 00:38:21 +0100658struct sk_buff *tipc_node_get_links(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100659{
660 u32 domain;
661 struct sk_buff *buf;
David S. Miller6c000552008-09-02 23:38:32 -0700662 struct tipc_node *n_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900663 struct tipc_link_info link_info;
Allan Stephensea138472006-06-29 12:33:20 -0700664 u32 payload_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100665
666 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NET_ADDR))
Per Liden4323add2006-01-18 00:38:21 +0100667 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100668
Al Viro3e6c8cd2006-11-08 00:19:09 -0800669 domain = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
Per Liden4323add2006-01-18 00:38:21 +0100670 if (!tipc_addr_domain_valid(domain))
671 return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
672 " (network address)");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100673
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900674 if (tipc_mode != TIPC_NET_MODE)
675 return tipc_cfg_reply_none();
676
Allan Stephens1aad72d2008-07-14 22:44:58 -0700677 read_lock_bh(&tipc_net_lock);
678
Allan Stephensea138472006-06-29 12:33:20 -0700679 /* Get space for all unicast links + multicast link */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100680
Allan Stephensea138472006-06-29 12:33:20 -0700681 payload_size = TLV_SPACE(sizeof(link_info)) *
682 (tipc_net.zones[tipc_zone(tipc_own_addr)]->links + 1);
Allan Stephens1aad72d2008-07-14 22:44:58 -0700683 if (payload_size > 32768u) {
684 read_unlock_bh(&tipc_net_lock);
Allan Stephensea138472006-06-29 12:33:20 -0700685 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
686 " (too many links)");
Allan Stephens1aad72d2008-07-14 22:44:58 -0700687 }
Allan Stephensea138472006-06-29 12:33:20 -0700688 buf = tipc_cfg_reply_alloc(payload_size);
Allan Stephens1aad72d2008-07-14 22:44:58 -0700689 if (!buf) {
690 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100691 return NULL;
Allan Stephens1aad72d2008-07-14 22:44:58 -0700692 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100693
694 /* Add TLV for broadcast link */
695
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900696 link_info.dest = htonl(tipc_own_addr & 0xfffff00);
697 link_info.up = htonl(1);
Stephen Hemminger4b704d52009-03-18 19:11:29 -0700698 strlcpy(link_info.str, tipc_bclink_name, TIPC_MAX_LINK_NAME);
Per Liden4323add2006-01-18 00:38:21 +0100699 tipc_cfg_append_tlv(buf, TIPC_TLV_LINK_INFO, &link_info, sizeof(link_info));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100700
701 /* Add TLVs for any other links in scope */
702
Per Liden4323add2006-01-18 00:38:21 +0100703 for (n_ptr = tipc_nodes; n_ptr; n_ptr = n_ptr->next) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900704 u32 i;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100705
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000706 if (!tipc_in_scope(domain, n_ptr->addr))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100707 continue;
Allan Stephens1aad72d2008-07-14 22:44:58 -0700708 tipc_node_lock(n_ptr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900709 for (i = 0; i < MAX_BEARERS; i++) {
710 if (!n_ptr->links[i])
711 continue;
712 link_info.dest = htonl(n_ptr->addr);
713 link_info.up = htonl(tipc_link_is_up(n_ptr->links[i]));
714 strcpy(link_info.str, n_ptr->links[i]->name);
715 tipc_cfg_append_tlv(buf, TIPC_TLV_LINK_INFO,
Per Liden4323add2006-01-18 00:38:21 +0100716 &link_info, sizeof(link_info));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900717 }
Allan Stephens1aad72d2008-07-14 22:44:58 -0700718 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100719 }
720
Allan Stephens1aad72d2008-07-14 22:44:58 -0700721 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100722 return buf;
723}