tipc: Convert node object array to a hash table
[deliverable/linux.git] / net / tipc / node.c
CommitLineData
b97bf3fd
PL
1/*
2 * net/tipc/node.c: TIPC node management routines
c4307285 3 *
593a5f22 4 * Copyright (c) 2000-2006, Ericsson AB
2d627b92 5 * Copyright (c) 2005-2006, 2010-2011, Wind River Systems
b97bf3fd
PL
6 * All rights reserved.
7 *
9ea1fd3c 8 * Redistribution and use in source and binary forms, with or without
b97bf3fd
PL
9 * modification, are permitted provided that the following conditions are met:
10 *
9ea1fd3c
PL
11 * 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.
b97bf3fd 19 *
9ea1fd3c
PL
20 * 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
b97bf3fd
PL
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37#include "core.h"
38#include "config.h"
39#include "node.h"
b97bf3fd 40#include "name_distr.h"
b97bf3fd 41
6c00055a
DM
42static void node_lost_contact(struct tipc_node *n_ptr);
43static void node_established_contact(struct tipc_node *n_ptr);
b97bf3fd 44
2ecb0924
AS
45static DEFINE_SPINLOCK(node_create_lock);
46
672d99e1
AS
47static struct hlist_head node_htable[NODE_HTABLE_SIZE];
48LIST_HEAD(tipc_node_list);
49static u32 tipc_num_nodes;
e3ec9c7d 50u32 tipc_own_tag;
b97bf3fd 51
672d99e1
AS
52/**
53 * tipc_node_find - locate specified node object, if it exists
54 */
55
56struct tipc_node *tipc_node_find(u32 addr)
57{
58 struct tipc_node *node;
59 struct hlist_node *pos;
60
61 if (unlikely(!in_own_cluster(addr)))
62 return NULL;
63
64 hlist_for_each_entry(node, pos, &node_htable[tipc_hashfn(addr)], hash) {
65 if (node->addr == addr)
66 return node;
67 }
68 return NULL;
69}
70
2ecb0924
AS
71/**
72 * tipc_node_create - create neighboring node
73 *
74 * Currently, this routine is called by neighbor discovery code, which holds
75 * net_lock for reading only. We must take node_create_lock to ensure a node
76 * isn't created twice if two different bearers discover the node at the same
77 * time. (It would be preferable to switch to holding net_lock in write mode,
78 * but this is a non-trivial change.)
79 */
80
6c00055a 81struct tipc_node *tipc_node_create(u32 addr)
b97bf3fd 82{
672d99e1 83 struct tipc_node *n_ptr, *temp_node;
b97bf3fd 84
2ecb0924
AS
85 spin_lock_bh(&node_create_lock);
86
5af54792
AS
87 n_ptr = tipc_node_find(addr);
88 if (n_ptr) {
89 spin_unlock_bh(&node_create_lock);
90 return n_ptr;
2ecb0924
AS
91 }
92
5af54792 93 n_ptr = kzalloc(sizeof(*n_ptr), GFP_ATOMIC);
a10bd924 94 if (!n_ptr) {
2ecb0924 95 spin_unlock_bh(&node_create_lock);
a10bd924
AS
96 warn("Node creation failed, no memory\n");
97 return NULL;
98 }
99
a10bd924 100 n_ptr->addr = addr;
51a8e4de 101 spin_lock_init(&n_ptr->lock);
672d99e1
AS
102 INIT_HLIST_NODE(&n_ptr->hash);
103 INIT_LIST_HEAD(&n_ptr->list);
a10bd924 104 INIT_LIST_HEAD(&n_ptr->nsub);
8f92df6a 105
672d99e1
AS
106 hlist_add_head(&n_ptr->hash, &node_htable[tipc_hashfn(addr)]);
107
108 list_for_each_entry(temp_node, &tipc_node_list, list) {
109 if (n_ptr->addr < temp_node->addr)
110 break;
111 }
112 list_add_tail(&n_ptr->list, &temp_node->list);
113
114 tipc_num_nodes++;
a10bd924 115
2ecb0924 116 spin_unlock_bh(&node_create_lock);
b97bf3fd
PL
117 return n_ptr;
118}
119
6c00055a 120void tipc_node_delete(struct tipc_node *n_ptr)
b97bf3fd 121{
672d99e1
AS
122 list_del(&n_ptr->list);
123 hlist_del(&n_ptr->hash);
b97bf3fd 124 kfree(n_ptr);
8f92df6a 125
672d99e1 126 tipc_num_nodes--;
b97bf3fd
PL
127}
128
129
130/**
4323add6 131 * tipc_node_link_up - handle addition of link
c4307285 132 *
b97bf3fd
PL
133 * Link becomes active (alone or shared) or standby, depending on its priority.
134 */
135
6c00055a 136void tipc_node_link_up(struct tipc_node *n_ptr, struct link *l_ptr)
b97bf3fd
PL
137{
138 struct link **active = &n_ptr->active_links[0];
139
5392d646
AS
140 n_ptr->working_links++;
141
b97bf3fd
PL
142 info("Established link <%s> on network plane %c\n",
143 l_ptr->name, l_ptr->b_ptr->net_plane);
c4307285 144
b97bf3fd 145 if (!active[0]) {
b97bf3fd
PL
146 active[0] = active[1] = l_ptr;
147 node_established_contact(n_ptr);
148 return;
149 }
c4307285 150 if (l_ptr->priority < active[0]->priority) {
a10bd924 151 info("New link <%s> becomes standby\n", l_ptr->name);
b97bf3fd
PL
152 return;
153 }
4323add6 154 tipc_link_send_duplicate(active[0], l_ptr);
c4307285 155 if (l_ptr->priority == active[0]->priority) {
b97bf3fd
PL
156 active[0] = l_ptr;
157 return;
158 }
a10bd924
AS
159 info("Old link <%s> becomes standby\n", active[0]->name);
160 if (active[1] != active[0])
161 info("Old link <%s> becomes standby\n", active[1]->name);
b97bf3fd
PL
162 active[0] = active[1] = l_ptr;
163}
164
165/**
166 * node_select_active_links - select active link
167 */
168
6c00055a 169static void node_select_active_links(struct tipc_node *n_ptr)
b97bf3fd
PL
170{
171 struct link **active = &n_ptr->active_links[0];
172 u32 i;
173 u32 highest_prio = 0;
174
c4307285 175 active[0] = active[1] = NULL;
b97bf3fd
PL
176
177 for (i = 0; i < MAX_BEARERS; i++) {
c4307285 178 struct link *l_ptr = n_ptr->links[i];
b97bf3fd 179
4323add6 180 if (!l_ptr || !tipc_link_is_up(l_ptr) ||
b97bf3fd
PL
181 (l_ptr->priority < highest_prio))
182 continue;
183
184 if (l_ptr->priority > highest_prio) {
c4307285 185 highest_prio = l_ptr->priority;
b97bf3fd
PL
186 active[0] = active[1] = l_ptr;
187 } else {
188 active[1] = l_ptr;
189 }
190 }
191}
192
193/**
4323add6 194 * tipc_node_link_down - handle loss of link
b97bf3fd
PL
195 */
196
6c00055a 197void tipc_node_link_down(struct tipc_node *n_ptr, struct link *l_ptr)
b97bf3fd
PL
198{
199 struct link **active;
200
5392d646
AS
201 n_ptr->working_links--;
202
4323add6 203 if (!tipc_link_is_active(l_ptr)) {
b97bf3fd
PL
204 info("Lost standby link <%s> on network plane %c\n",
205 l_ptr->name, l_ptr->b_ptr->net_plane);
206 return;
207 }
208 info("Lost link <%s> on network plane %c\n",
209 l_ptr->name, l_ptr->b_ptr->net_plane);
210
211 active = &n_ptr->active_links[0];
212 if (active[0] == l_ptr)
213 active[0] = active[1];
214 if (active[1] == l_ptr)
215 active[1] = active[0];
216 if (active[0] == l_ptr)
217 node_select_active_links(n_ptr);
c4307285 218 if (tipc_node_is_up(n_ptr))
4323add6 219 tipc_link_changeover(l_ptr);
c4307285 220 else
b97bf3fd
PL
221 node_lost_contact(n_ptr);
222}
223
6c00055a 224int tipc_node_has_active_links(struct tipc_node *n_ptr)
b97bf3fd 225{
76ae0d71 226 return n_ptr->active_links[0] != NULL;
b97bf3fd
PL
227}
228
6c00055a 229int tipc_node_has_redundant_links(struct tipc_node *n_ptr)
b97bf3fd 230{
a02cec21 231 return n_ptr->working_links > 1;
b97bf3fd
PL
232}
233
6c00055a 234int tipc_node_is_up(struct tipc_node *n_ptr)
b97bf3fd 235{
51a8e4de 236 return tipc_node_has_active_links(n_ptr);
b97bf3fd
PL
237}
238
6c00055a 239struct tipc_node *tipc_node_attach_link(struct link *l_ptr)
b97bf3fd 240{
6c00055a 241 struct tipc_node *n_ptr = tipc_node_find(l_ptr->addr);
b97bf3fd
PL
242
243 if (!n_ptr)
4323add6 244 n_ptr = tipc_node_create(l_ptr->addr);
c4307285 245 if (n_ptr) {
b97bf3fd
PL
246 u32 bearer_id = l_ptr->b_ptr->identity;
247 char addr_string[16];
248
c4307285 249 if (n_ptr->link_cnt >= 2) {
c4307285 250 err("Attempt to create third link to %s\n",
c68ca7b7 251 tipc_addr_string_fill(addr_string, n_ptr->addr));
c4307285
YH
252 return NULL;
253 }
254
255 if (!n_ptr->links[bearer_id]) {
256 n_ptr->links[bearer_id] = l_ptr;
d1bcb115 257 atomic_inc(&tipc_num_links);
c4307285
YH
258 n_ptr->link_cnt++;
259 return n_ptr;
260 }
a570f095 261 err("Attempt to establish second link on <%s> to %s\n",
2d627b92 262 l_ptr->b_ptr->name,
c68ca7b7 263 tipc_addr_string_fill(addr_string, l_ptr->addr));
c4307285 264 }
1fc54d8f 265 return NULL;
b97bf3fd
PL
266}
267
6c00055a 268void tipc_node_detach_link(struct tipc_node *n_ptr, struct link *l_ptr)
b97bf3fd 269{
1fc54d8f 270 n_ptr->links[l_ptr->b_ptr->identity] = NULL;
d1bcb115 271 atomic_dec(&tipc_num_links);
b97bf3fd
PL
272 n_ptr->link_cnt--;
273}
274
275/*
276 * Routing table management - five cases to handle:
277 *
278 * 1: A link towards a zone/cluster external node comes up.
c4307285
YH
279 * => Send a multicast message updating routing tables of all
280 * system nodes within own cluster that the new destination
281 * can be reached via this node.
b97bf3fd
PL
282 * (node.establishedContact()=>cluster.multicastNewRoute())
283 *
284 * 2: A link towards a slave node comes up.
c4307285
YH
285 * => Send a multicast message updating routing tables of all
286 * system nodes within own cluster that the new destination
287 * can be reached via this node.
b97bf3fd 288 * (node.establishedContact()=>cluster.multicastNewRoute())
c4307285 289 * => Send a message to the slave node about existence
b97bf3fd
PL
290 * of all system nodes within cluster:
291 * (node.establishedContact()=>cluster.sendLocalRoutes())
292 *
293 * 3: A new cluster local system node becomes available.
294 * => Send message(s) to this particular node containing
295 * information about all cluster external and slave
296 * nodes which can be reached via this node.
297 * (node.establishedContact()==>network.sendExternalRoutes())
298 * (node.establishedContact()==>network.sendSlaveRoutes())
c4307285 299 * => Send messages to all directly connected slave nodes
b97bf3fd
PL
300 * containing information about the existence of the new node
301 * (node.establishedContact()=>cluster.multicastNewRoute())
c4307285 302 *
b97bf3fd
PL
303 * 4: The link towards a zone/cluster external node or slave
304 * node goes down.
c4307285 305 * => Send a multcast message updating routing tables of all
b97bf3fd
PL
306 * nodes within cluster that the new destination can not any
307 * longer be reached via this node.
308 * (node.lostAllLinks()=>cluster.bcastLostRoute())
309 *
310 * 5: A cluster local system node becomes unavailable.
311 * => Remove all references to this node from the local
312 * routing tables. Note: This is a completely node
313 * local operation.
314 * (node.lostAllLinks()=>network.removeAsRouter())
c4307285 315 * => Send messages to all directly connected slave nodes
b97bf3fd
PL
316 * containing information about loss of the node
317 * (node.establishedContact()=>cluster.multicastLostRoute())
318 *
319 */
320
6c00055a 321static void node_established_contact(struct tipc_node *n_ptr)
b97bf3fd 322{
51a8e4de 323 tipc_k_signal((Handler)tipc_named_node_up, n_ptr->addr);
b97bf3fd 324
c4307285
YH
325 /* Syncronize broadcast acks */
326 n_ptr->bclink.acked = tipc_bclink_get_last_sent();
b97bf3fd 327
b97bf3fd 328 if (n_ptr->bclink.supported) {
8f92df6a 329 tipc_nmap_add(&tipc_bcast_nmap, n_ptr->addr);
b97bf3fd
PL
330 if (n_ptr->addr < tipc_own_addr)
331 tipc_own_tag++;
332 }
b97bf3fd
PL
333}
334
5a68d5ee
AS
335static void node_cleanup_finished(unsigned long node_addr)
336{
337 struct tipc_node *n_ptr;
338
339 read_lock_bh(&tipc_net_lock);
340 n_ptr = tipc_node_find(node_addr);
341 if (n_ptr) {
342 tipc_node_lock(n_ptr);
343 n_ptr->cleanup_required = 0;
344 tipc_node_unlock(n_ptr);
345 }
346 read_unlock_bh(&tipc_net_lock);
347}
348
6c00055a 349static void node_lost_contact(struct tipc_node *n_ptr)
b97bf3fd 350{
b97bf3fd
PL
351 char addr_string[16];
352 u32 i;
353
c4307285
YH
354 /* Clean up broadcast reception remains */
355 n_ptr->bclink.gap_after = n_ptr->bclink.gap_to = 0;
356 while (n_ptr->bclink.deferred_head) {
0e65967e 357 struct sk_buff *buf = n_ptr->bclink.deferred_head;
c4307285
YH
358 n_ptr->bclink.deferred_head = buf->next;
359 buf_discard(buf);
360 }
361 if (n_ptr->bclink.defragm) {
362 buf_discard(n_ptr->bclink.defragm);
363 n_ptr->bclink.defragm = NULL;
364 }
c4307285 365
51a8e4de 366 if (n_ptr->bclink.supported) {
8f92df6a
AS
367 tipc_bclink_acknowledge(n_ptr,
368 mod(n_ptr->bclink.acked + 10000));
369 tipc_nmap_remove(&tipc_bcast_nmap, n_ptr->addr);
51a8e4de
AS
370 if (n_ptr->addr < tipc_own_addr)
371 tipc_own_tag--;
b97bf3fd 372 }
b97bf3fd 373
c4307285 374 info("Lost contact with %s\n",
c68ca7b7 375 tipc_addr_string_fill(addr_string, n_ptr->addr));
b97bf3fd
PL
376
377 /* Abort link changeover */
378 for (i = 0; i < MAX_BEARERS; i++) {
379 struct link *l_ptr = n_ptr->links[i];
c4307285 380 if (!l_ptr)
b97bf3fd
PL
381 continue;
382 l_ptr->reset_checkpoint = l_ptr->next_in_no;
383 l_ptr->exp_msg_count = 0;
4323add6 384 tipc_link_reset_fragments(l_ptr);
b97bf3fd
PL
385 }
386
387 /* Notify subscribers */
f1379173 388 tipc_nodesub_notify(n_ptr);
5a68d5ee
AS
389
390 /* Prevent re-contact with node until all cleanup is done */
391
392 n_ptr->cleanup_required = 1;
393 tipc_k_signal((Handler)node_cleanup_finished, n_ptr->addr);
b97bf3fd
PL
394}
395
4323add6 396struct sk_buff *tipc_node_get_nodes(const void *req_tlv_area, int req_tlv_space)
b97bf3fd
PL
397{
398 u32 domain;
399 struct sk_buff *buf;
6c00055a 400 struct tipc_node *n_ptr;
c4307285 401 struct tipc_node_info node_info;
ea13847b 402 u32 payload_size;
b97bf3fd
PL
403
404 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NET_ADDR))
4323add6 405 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
b97bf3fd 406
3e6c8cd5 407 domain = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
4323add6
PL
408 if (!tipc_addr_domain_valid(domain))
409 return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
410 " (network address)");
b97bf3fd 411
1aad72d6 412 read_lock_bh(&tipc_net_lock);
672d99e1 413 if (!tipc_num_nodes) {
1aad72d6 414 read_unlock_bh(&tipc_net_lock);
c4307285 415 return tipc_cfg_reply_none();
1aad72d6 416 }
b97bf3fd 417
08c80e9a 418 /* For now, get space for all other nodes */
b97bf3fd 419
672d99e1 420 payload_size = TLV_SPACE(sizeof(node_info)) * tipc_num_nodes;
1aad72d6
AS
421 if (payload_size > 32768u) {
422 read_unlock_bh(&tipc_net_lock);
ea13847b
AS
423 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
424 " (too many nodes)");
1aad72d6 425 }
ea13847b 426 buf = tipc_cfg_reply_alloc(payload_size);
1aad72d6
AS
427 if (!buf) {
428 read_unlock_bh(&tipc_net_lock);
b97bf3fd 429 return NULL;
1aad72d6 430 }
b97bf3fd
PL
431
432 /* Add TLVs for all nodes in scope */
433
672d99e1
AS
434 list_for_each_entry(n_ptr, &tipc_node_list, list) {
435 if (!tipc_in_scope(domain, n_ptr->addr))
b97bf3fd 436 continue;
c4307285
YH
437 node_info.addr = htonl(n_ptr->addr);
438 node_info.up = htonl(tipc_node_is_up(n_ptr));
439 tipc_cfg_append_tlv(buf, TIPC_TLV_NODE_INFO,
4323add6 440 &node_info, sizeof(node_info));
b97bf3fd
PL
441 }
442
1aad72d6 443 read_unlock_bh(&tipc_net_lock);
b97bf3fd
PL
444 return buf;
445}
446
4323add6 447struct sk_buff *tipc_node_get_links(const void *req_tlv_area, int req_tlv_space)
b97bf3fd
PL
448{
449 u32 domain;
450 struct sk_buff *buf;
6c00055a 451 struct tipc_node *n_ptr;
c4307285 452 struct tipc_link_info link_info;
ea13847b 453 u32 payload_size;
b97bf3fd
PL
454
455 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NET_ADDR))
4323add6 456 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
b97bf3fd 457
3e6c8cd5 458 domain = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
4323add6
PL
459 if (!tipc_addr_domain_valid(domain))
460 return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
461 " (network address)");
b97bf3fd 462
c4307285
YH
463 if (tipc_mode != TIPC_NET_MODE)
464 return tipc_cfg_reply_none();
465
1aad72d6
AS
466 read_lock_bh(&tipc_net_lock);
467
ea13847b
AS
468 /* Get space for all unicast links + multicast link */
469
9df3b7eb 470 payload_size = TLV_SPACE(sizeof(link_info)) *
d1bcb115 471 (atomic_read(&tipc_num_links) + 1);
1aad72d6
AS
472 if (payload_size > 32768u) {
473 read_unlock_bh(&tipc_net_lock);
ea13847b
AS
474 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
475 " (too many links)");
1aad72d6 476 }
ea13847b 477 buf = tipc_cfg_reply_alloc(payload_size);
1aad72d6
AS
478 if (!buf) {
479 read_unlock_bh(&tipc_net_lock);
b97bf3fd 480 return NULL;
1aad72d6 481 }
b97bf3fd
PL
482
483 /* Add TLV for broadcast link */
484
a3796f89 485 link_info.dest = htonl(tipc_cluster_mask(tipc_own_addr));
c4307285 486 link_info.up = htonl(1);
4b704d59 487 strlcpy(link_info.str, tipc_bclink_name, TIPC_MAX_LINK_NAME);
4323add6 488 tipc_cfg_append_tlv(buf, TIPC_TLV_LINK_INFO, &link_info, sizeof(link_info));
b97bf3fd
PL
489
490 /* Add TLVs for any other links in scope */
491
672d99e1 492 list_for_each_entry(n_ptr, &tipc_node_list, list) {
c4307285 493 u32 i;
b97bf3fd 494
672d99e1 495 if (!tipc_in_scope(domain, n_ptr->addr))
b97bf3fd 496 continue;
1aad72d6 497 tipc_node_lock(n_ptr);
c4307285
YH
498 for (i = 0; i < MAX_BEARERS; i++) {
499 if (!n_ptr->links[i])
500 continue;
501 link_info.dest = htonl(n_ptr->addr);
502 link_info.up = htonl(tipc_link_is_up(n_ptr->links[i]));
503 strcpy(link_info.str, n_ptr->links[i]->name);
504 tipc_cfg_append_tlv(buf, TIPC_TLV_LINK_INFO,
4323add6 505 &link_info, sizeof(link_info));
c4307285 506 }
1aad72d6 507 tipc_node_unlock(n_ptr);
b97bf3fd
PL
508 }
509
1aad72d6 510 read_unlock_bh(&tipc_net_lock);
b97bf3fd
PL
511 return buf;
512}
This page took 0.486839 seconds and 5 git commands to generate.