tipc: rename node create lock to protect node list and hlist
[deliverable/linux.git] / net / tipc / node.c
CommitLineData
b97bf3fd
PL
1/*
2 * net/tipc/node.c: TIPC node management routines
c4307285 3 *
c64f7a6a 4 * Copyright (c) 2000-2006, 2012 Ericsson AB
46651c59 5 * Copyright (c) 2005-2006, 2010-2014, 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
a635b46b
AS
42#define NODE_HTABLE_SIZE 512
43
6c00055a
DM
44static void node_lost_contact(struct tipc_node *n_ptr);
45static void node_established_contact(struct tipc_node *n_ptr);
b97bf3fd 46
672d99e1
AS
47static struct hlist_head node_htable[NODE_HTABLE_SIZE];
48LIST_HEAD(tipc_node_list);
49static u32 tipc_num_nodes;
46651c59 50static DEFINE_SPINLOCK(node_list_lock);
34e46258
AS
51
52static atomic_t tipc_num_links = ATOMIC_INIT(0);
b97bf3fd 53
a635b46b
AS
54/*
55 * A trivial power-of-two bitmask technique is used for speed, since this
56 * operation is done for every incoming TIPC packet. The number of hash table
57 * entries has been chosen so that no hash chain exceeds 8 nodes and will
58 * usually be much smaller (typically only a single node).
59 */
872f24db 60static unsigned int tipc_hashfn(u32 addr)
a635b46b
AS
61{
62 return addr & (NODE_HTABLE_SIZE - 1);
63}
64
1ec2bb08 65/*
672d99e1
AS
66 * tipc_node_find - locate specified node object, if it exists
67 */
672d99e1
AS
68struct tipc_node *tipc_node_find(u32 addr)
69{
70 struct tipc_node *node;
672d99e1 71
336ebf5b 72 if (unlikely(!in_own_cluster_exact(addr)))
672d99e1
AS
73 return NULL;
74
46651c59 75 spin_lock_bh(&node_list_lock);
b67bfe0d 76 hlist_for_each_entry(node, &node_htable[tipc_hashfn(addr)], hash) {
46651c59
YX
77 if (node->addr == addr) {
78 spin_unlock_bh(&node_list_lock);
672d99e1 79 return node;
46651c59 80 }
672d99e1 81 }
46651c59 82 spin_unlock_bh(&node_list_lock);
672d99e1
AS
83 return NULL;
84}
85
6c00055a 86struct tipc_node *tipc_node_create(u32 addr)
b97bf3fd 87{
672d99e1 88 struct tipc_node *n_ptr, *temp_node;
b97bf3fd 89
46651c59 90 spin_lock_bh(&node_list_lock);
2ecb0924 91
5af54792 92 n_ptr = kzalloc(sizeof(*n_ptr), GFP_ATOMIC);
a10bd924 93 if (!n_ptr) {
46651c59 94 spin_unlock_bh(&node_list_lock);
2cf8aa19 95 pr_warn("Node creation failed, no memory\n");
a10bd924
AS
96 return NULL;
97 }
98
a10bd924 99 n_ptr->addr = addr;
51a8e4de 100 spin_lock_init(&n_ptr->lock);
672d99e1
AS
101 INIT_HLIST_NODE(&n_ptr->hash);
102 INIT_LIST_HEAD(&n_ptr->list);
a10bd924 103 INIT_LIST_HEAD(&n_ptr->nsub);
8f92df6a 104
672d99e1
AS
105 hlist_add_head(&n_ptr->hash, &node_htable[tipc_hashfn(addr)]);
106
107 list_for_each_entry(temp_node, &tipc_node_list, list) {
108 if (n_ptr->addr < temp_node->addr)
109 break;
110 }
111 list_add_tail(&n_ptr->list, &temp_node->list);
b4b56102 112 n_ptr->block_setup = WAIT_PEER_DOWN;
fc0eea69 113 n_ptr->signature = INVALID_NODE_SIG;
672d99e1
AS
114
115 tipc_num_nodes++;
a10bd924 116
46651c59 117 spin_unlock_bh(&node_list_lock);
b97bf3fd
PL
118 return n_ptr;
119}
120
46651c59 121static void tipc_node_delete(struct tipc_node *n_ptr)
b97bf3fd 122{
672d99e1
AS
123 list_del(&n_ptr->list);
124 hlist_del(&n_ptr->hash);
b97bf3fd 125 kfree(n_ptr);
8f92df6a 126
672d99e1 127 tipc_num_nodes--;
b97bf3fd
PL
128}
129
46651c59
YX
130void tipc_node_stop(void)
131{
132 struct tipc_node *node, *t_node;
133
134 spin_lock_bh(&node_list_lock);
135 list_for_each_entry_safe(node, t_node, &tipc_node_list, list)
136 tipc_node_delete(node);
137 spin_unlock_bh(&node_list_lock);
138}
139
b97bf3fd 140/**
4323add6 141 * tipc_node_link_up - handle addition of link
c4307285 142 *
b97bf3fd
PL
143 * Link becomes active (alone or shared) or standby, depending on its priority.
144 */
a18c4bc3 145void tipc_node_link_up(struct tipc_node *n_ptr, struct tipc_link *l_ptr)
b97bf3fd 146{
a18c4bc3 147 struct tipc_link **active = &n_ptr->active_links[0];
b97bf3fd 148
5392d646
AS
149 n_ptr->working_links++;
150
2cf8aa19
EH
151 pr_info("Established link <%s> on network plane %c\n",
152 l_ptr->name, l_ptr->b_ptr->net_plane);
c4307285 153
b97bf3fd 154 if (!active[0]) {
b97bf3fd
PL
155 active[0] = active[1] = l_ptr;
156 node_established_contact(n_ptr);
157 return;
158 }
c4307285 159 if (l_ptr->priority < active[0]->priority) {
2cf8aa19 160 pr_info("New link <%s> becomes standby\n", l_ptr->name);
b97bf3fd
PL
161 return;
162 }
247f0f3c 163 tipc_link_dup_queue_xmit(active[0], l_ptr);
c4307285 164 if (l_ptr->priority == active[0]->priority) {
b97bf3fd
PL
165 active[0] = l_ptr;
166 return;
167 }
2cf8aa19 168 pr_info("Old link <%s> becomes standby\n", active[0]->name);
a10bd924 169 if (active[1] != active[0])
2cf8aa19 170 pr_info("Old link <%s> becomes standby\n", active[1]->name);
b97bf3fd
PL
171 active[0] = active[1] = l_ptr;
172}
173
174/**
175 * node_select_active_links - select active link
176 */
6c00055a 177static void node_select_active_links(struct tipc_node *n_ptr)
b97bf3fd 178{
a18c4bc3 179 struct tipc_link **active = &n_ptr->active_links[0];
b97bf3fd
PL
180 u32 i;
181 u32 highest_prio = 0;
182
c4307285 183 active[0] = active[1] = NULL;
b97bf3fd
PL
184
185 for (i = 0; i < MAX_BEARERS; i++) {
a18c4bc3 186 struct tipc_link *l_ptr = n_ptr->links[i];
b97bf3fd 187
4323add6 188 if (!l_ptr || !tipc_link_is_up(l_ptr) ||
b97bf3fd
PL
189 (l_ptr->priority < highest_prio))
190 continue;
191
192 if (l_ptr->priority > highest_prio) {
c4307285 193 highest_prio = l_ptr->priority;
b97bf3fd
PL
194 active[0] = active[1] = l_ptr;
195 } else {
196 active[1] = l_ptr;
197 }
198 }
199}
200
201/**
4323add6 202 * tipc_node_link_down - handle loss of link
b97bf3fd 203 */
a18c4bc3 204void tipc_node_link_down(struct tipc_node *n_ptr, struct tipc_link *l_ptr)
b97bf3fd 205{
a18c4bc3 206 struct tipc_link **active;
b97bf3fd 207
5392d646
AS
208 n_ptr->working_links--;
209
4323add6 210 if (!tipc_link_is_active(l_ptr)) {
2cf8aa19
EH
211 pr_info("Lost standby link <%s> on network plane %c\n",
212 l_ptr->name, l_ptr->b_ptr->net_plane);
b97bf3fd
PL
213 return;
214 }
2cf8aa19 215 pr_info("Lost link <%s> on network plane %c\n",
b97bf3fd
PL
216 l_ptr->name, l_ptr->b_ptr->net_plane);
217
218 active = &n_ptr->active_links[0];
219 if (active[0] == l_ptr)
220 active[0] = active[1];
221 if (active[1] == l_ptr)
222 active[1] = active[0];
223 if (active[0] == l_ptr)
224 node_select_active_links(n_ptr);
c4307285 225 if (tipc_node_is_up(n_ptr))
170b3927 226 tipc_link_failover_send_queue(l_ptr);
c4307285 227 else
b97bf3fd
PL
228 node_lost_contact(n_ptr);
229}
230
8f19afb2 231int tipc_node_active_links(struct tipc_node *n_ptr)
b97bf3fd 232{
76ae0d71 233 return n_ptr->active_links[0] != NULL;
b97bf3fd
PL
234}
235
6c00055a 236int tipc_node_is_up(struct tipc_node *n_ptr)
b97bf3fd 237{
8f19afb2 238 return tipc_node_active_links(n_ptr);
b97bf3fd
PL
239}
240
a18c4bc3 241void tipc_node_attach_link(struct tipc_node *n_ptr, struct tipc_link *l_ptr)
b97bf3fd 242{
37b9c08a
AS
243 n_ptr->links[l_ptr->b_ptr->identity] = l_ptr;
244 atomic_inc(&tipc_num_links);
245 n_ptr->link_cnt++;
b97bf3fd
PL
246}
247
a18c4bc3 248void tipc_node_detach_link(struct tipc_node *n_ptr, struct tipc_link *l_ptr)
b97bf3fd 249{
7d33939f
JPM
250 int i;
251
252 for (i = 0; i < MAX_BEARERS; i++) {
074bb43e
JPM
253 if (l_ptr != n_ptr->links[i])
254 continue;
255 n_ptr->links[i] = NULL;
256 atomic_dec(&tipc_num_links);
257 n_ptr->link_cnt--;
7d33939f 258 }
b97bf3fd
PL
259}
260
6c00055a 261static void node_established_contact(struct tipc_node *n_ptr)
b97bf3fd 262{
51a8e4de 263 tipc_k_signal((Handler)tipc_named_node_up, n_ptr->addr);
c64f7a6a 264 n_ptr->bclink.oos_state = 0;
818f4da5
YX
265 n_ptr->bclink.acked = tipc_bclink_get_last_sent();
266 tipc_bclink_add_node(n_ptr->addr);
b97bf3fd
PL
267}
268
b4b56102 269static void node_name_purge_complete(unsigned long node_addr)
5a68d5ee
AS
270{
271 struct tipc_node *n_ptr;
272
273 read_lock_bh(&tipc_net_lock);
274 n_ptr = tipc_node_find(node_addr);
275 if (n_ptr) {
276 tipc_node_lock(n_ptr);
b4b56102 277 n_ptr->block_setup &= ~WAIT_NAMES_GONE;
5a68d5ee
AS
278 tipc_node_unlock(n_ptr);
279 }
280 read_unlock_bh(&tipc_net_lock);
281}
282
6c00055a 283static void node_lost_contact(struct tipc_node *n_ptr)
b97bf3fd 284{
b97bf3fd
PL
285 char addr_string[16];
286 u32 i;
287
2cf8aa19
EH
288 pr_info("Lost contact with %s\n",
289 tipc_addr_string_fill(addr_string, n_ptr->addr));
c5bd4d85
AS
290
291 /* Flush broadcast link info associated with lost node */
389dd9bc 292 if (n_ptr->bclink.recv_permitted) {
d77b3831 293 kfree_skb_list(n_ptr->bclink.deferred_head);
7a54d4a9 294 n_ptr->bclink.deferred_size = 0;
c5bd4d85 295
40ba3cdf
EH
296 if (n_ptr->bclink.reasm_head) {
297 kfree_skb(n_ptr->bclink.reasm_head);
298 n_ptr->bclink.reasm_head = NULL;
299 n_ptr->bclink.reasm_tail = NULL;
c5bd4d85
AS
300 }
301
cd3decdf 302 tipc_bclink_remove_node(n_ptr->addr);
36559591 303 tipc_bclink_acknowledge(n_ptr, INVALID_LINK_SEQ);
b97bf3fd 304
389dd9bc 305 n_ptr->bclink.recv_permitted = false;
c5bd4d85 306 }
b97bf3fd
PL
307
308 /* Abort link changeover */
309 for (i = 0; i < MAX_BEARERS; i++) {
a18c4bc3 310 struct tipc_link *l_ptr = n_ptr->links[i];
c4307285 311 if (!l_ptr)
b97bf3fd
PL
312 continue;
313 l_ptr->reset_checkpoint = l_ptr->next_in_no;
314 l_ptr->exp_msg_count = 0;
4323add6 315 tipc_link_reset_fragments(l_ptr);
b97bf3fd
PL
316 }
317
318 /* Notify subscribers */
f1379173 319 tipc_nodesub_notify(n_ptr);
5a68d5ee 320
b4b56102 321 /* Prevent re-contact with node until cleanup is done */
b4b56102
AS
322 n_ptr->block_setup = WAIT_PEER_DOWN | WAIT_NAMES_GONE;
323 tipc_k_signal((Handler)node_name_purge_complete, n_ptr->addr);
b97bf3fd
PL
324}
325
4323add6 326struct sk_buff *tipc_node_get_nodes(const void *req_tlv_area, int req_tlv_space)
b97bf3fd
PL
327{
328 u32 domain;
329 struct sk_buff *buf;
6c00055a 330 struct tipc_node *n_ptr;
c4307285 331 struct tipc_node_info node_info;
ea13847b 332 u32 payload_size;
b97bf3fd
PL
333
334 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NET_ADDR))
4323add6 335 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
b97bf3fd 336
3e6c8cd5 337 domain = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
4323add6
PL
338 if (!tipc_addr_domain_valid(domain))
339 return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
340 " (network address)");
b97bf3fd 341
46651c59 342 spin_lock_bh(&node_list_lock);
672d99e1 343 if (!tipc_num_nodes) {
46651c59 344 spin_unlock_bh(&node_list_lock);
c4307285 345 return tipc_cfg_reply_none();
1aad72d6 346 }
b97bf3fd 347
08c80e9a 348 /* For now, get space for all other nodes */
672d99e1 349 payload_size = TLV_SPACE(sizeof(node_info)) * tipc_num_nodes;
1aad72d6 350 if (payload_size > 32768u) {
46651c59 351 spin_unlock_bh(&node_list_lock);
ea13847b
AS
352 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
353 " (too many nodes)");
1aad72d6 354 }
ea13847b 355 buf = tipc_cfg_reply_alloc(payload_size);
1aad72d6 356 if (!buf) {
46651c59 357 spin_unlock_bh(&node_list_lock);
b97bf3fd 358 return NULL;
1aad72d6 359 }
b97bf3fd
PL
360
361 /* Add TLVs for all nodes in scope */
672d99e1
AS
362 list_for_each_entry(n_ptr, &tipc_node_list, list) {
363 if (!tipc_in_scope(domain, n_ptr->addr))
b97bf3fd 364 continue;
c4307285
YH
365 node_info.addr = htonl(n_ptr->addr);
366 node_info.up = htonl(tipc_node_is_up(n_ptr));
367 tipc_cfg_append_tlv(buf, TIPC_TLV_NODE_INFO,
4323add6 368 &node_info, sizeof(node_info));
b97bf3fd 369 }
46651c59 370 spin_unlock_bh(&node_list_lock);
b97bf3fd
PL
371 return buf;
372}
373
4323add6 374struct sk_buff *tipc_node_get_links(const void *req_tlv_area, int req_tlv_space)
b97bf3fd
PL
375{
376 u32 domain;
377 struct sk_buff *buf;
6c00055a 378 struct tipc_node *n_ptr;
c4307285 379 struct tipc_link_info link_info;
ea13847b 380 u32 payload_size;
b97bf3fd
PL
381
382 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NET_ADDR))
4323add6 383 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
b97bf3fd 384
3e6c8cd5 385 domain = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
4323add6
PL
386 if (!tipc_addr_domain_valid(domain))
387 return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
388 " (network address)");
b97bf3fd 389
b58343f9 390 if (!tipc_own_addr)
c4307285
YH
391 return tipc_cfg_reply_none();
392
46651c59 393 spin_lock_bh(&node_list_lock);
7a54d4a9 394 /* Get space for all unicast links + broadcast link */
9df3b7eb 395 payload_size = TLV_SPACE(sizeof(link_info)) *
d1bcb115 396 (atomic_read(&tipc_num_links) + 1);
1aad72d6 397 if (payload_size > 32768u) {
46651c59 398 spin_unlock_bh(&node_list_lock);
ea13847b
AS
399 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
400 " (too many links)");
1aad72d6 401 }
ea13847b 402 buf = tipc_cfg_reply_alloc(payload_size);
1aad72d6 403 if (!buf) {
46651c59 404 spin_unlock_bh(&node_list_lock);
b97bf3fd 405 return NULL;
1aad72d6 406 }
b97bf3fd
PL
407
408 /* Add TLV for broadcast link */
a3796f89 409 link_info.dest = htonl(tipc_cluster_mask(tipc_own_addr));
c4307285 410 link_info.up = htonl(1);
4b704d59 411 strlcpy(link_info.str, tipc_bclink_name, TIPC_MAX_LINK_NAME);
4323add6 412 tipc_cfg_append_tlv(buf, TIPC_TLV_LINK_INFO, &link_info, sizeof(link_info));
b97bf3fd
PL
413
414 /* Add TLVs for any other links in scope */
672d99e1 415 list_for_each_entry(n_ptr, &tipc_node_list, list) {
c4307285 416 u32 i;
b97bf3fd 417
672d99e1 418 if (!tipc_in_scope(domain, n_ptr->addr))
b97bf3fd 419 continue;
1aad72d6 420 tipc_node_lock(n_ptr);
c4307285
YH
421 for (i = 0; i < MAX_BEARERS; i++) {
422 if (!n_ptr->links[i])
423 continue;
424 link_info.dest = htonl(n_ptr->addr);
425 link_info.up = htonl(tipc_link_is_up(n_ptr->links[i]));
426 strcpy(link_info.str, n_ptr->links[i]->name);
427 tipc_cfg_append_tlv(buf, TIPC_TLV_LINK_INFO,
4323add6 428 &link_info, sizeof(link_info));
c4307285 429 }
1aad72d6 430 tipc_node_unlock(n_ptr);
b97bf3fd 431 }
46651c59 432 spin_unlock_bh(&node_list_lock);
b97bf3fd
PL
433 return buf;
434}
This page took 0.653757 seconds and 5 git commands to generate.