2 * net/tipc/node.c: TIPC node management routines
4 * Copyright (c) 2000-2006, 2012-2014, Ericsson AB
5 * Copyright (c) 2005-2006, 2010-2014, Wind River Systems
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
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.
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.
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
34 * POSSIBILITY OF SUCH DAMAGE.
40 #include "name_distr.h"
43 #define NODE_HTABLE_SIZE 512
45 static void node_lost_contact(struct tipc_node
*n_ptr
);
46 static void node_established_contact(struct tipc_node
*n_ptr
);
48 static struct hlist_head node_htable
[NODE_HTABLE_SIZE
];
49 LIST_HEAD(tipc_node_list
);
50 static u32 tipc_num_nodes
;
51 static u32 tipc_num_links
;
52 static DEFINE_SPINLOCK(node_list_lock
);
54 struct tipc_sock_conn
{
58 struct list_head list
;
62 * A trivial power-of-two bitmask technique is used for speed, since this
63 * operation is done for every incoming TIPC packet. The number of hash table
64 * entries has been chosen so that no hash chain exceeds 8 nodes and will
65 * usually be much smaller (typically only a single node).
67 static unsigned int tipc_hashfn(u32 addr
)
69 return addr
& (NODE_HTABLE_SIZE
- 1);
73 * tipc_node_find - locate specified node object, if it exists
75 struct tipc_node
*tipc_node_find(u32 addr
)
77 struct tipc_node
*node
;
79 if (unlikely(!in_own_cluster_exact(addr
)))
83 hlist_for_each_entry_rcu(node
, &node_htable
[tipc_hashfn(addr
)], hash
) {
84 if (node
->addr
== addr
) {
93 struct tipc_node
*tipc_node_create(u32 addr
)
95 struct tipc_node
*n_ptr
, *temp_node
;
97 spin_lock_bh(&node_list_lock
);
99 n_ptr
= kzalloc(sizeof(*n_ptr
), GFP_ATOMIC
);
101 spin_unlock_bh(&node_list_lock
);
102 pr_warn("Node creation failed, no memory\n");
107 spin_lock_init(&n_ptr
->lock
);
108 INIT_HLIST_NODE(&n_ptr
->hash
);
109 INIT_LIST_HEAD(&n_ptr
->list
);
110 INIT_LIST_HEAD(&n_ptr
->nsub
);
111 INIT_LIST_HEAD(&n_ptr
->conn_sks
);
112 __skb_queue_head_init(&n_ptr
->waiting_sks
);
114 hlist_add_head_rcu(&n_ptr
->hash
, &node_htable
[tipc_hashfn(addr
)]);
116 list_for_each_entry_rcu(temp_node
, &tipc_node_list
, list
) {
117 if (n_ptr
->addr
< temp_node
->addr
)
120 list_add_tail_rcu(&n_ptr
->list
, &temp_node
->list
);
121 n_ptr
->action_flags
= TIPC_WAIT_PEER_LINKS_DOWN
;
122 n_ptr
->signature
= INVALID_NODE_SIG
;
126 spin_unlock_bh(&node_list_lock
);
130 static void tipc_node_delete(struct tipc_node
*n_ptr
)
132 list_del_rcu(&n_ptr
->list
);
133 hlist_del_rcu(&n_ptr
->hash
);
134 kfree_rcu(n_ptr
, rcu
);
139 void tipc_node_stop(void)
141 struct tipc_node
*node
, *t_node
;
143 spin_lock_bh(&node_list_lock
);
144 list_for_each_entry_safe(node
, t_node
, &tipc_node_list
, list
)
145 tipc_node_delete(node
);
146 spin_unlock_bh(&node_list_lock
);
149 int tipc_node_add_conn(u32 dnode
, u32 port
, u32 peer_port
)
151 struct tipc_node
*node
;
152 struct tipc_sock_conn
*conn
;
154 if (in_own_node(dnode
))
157 node
= tipc_node_find(dnode
);
159 pr_warn("Connecting sock to node 0x%x failed\n", dnode
);
160 return -EHOSTUNREACH
;
162 conn
= kmalloc(sizeof(*conn
), GFP_ATOMIC
);
164 return -EHOSTUNREACH
;
165 conn
->peer_node
= dnode
;
167 conn
->peer_port
= peer_port
;
169 tipc_node_lock(node
);
170 list_add_tail(&conn
->list
, &node
->conn_sks
);
171 tipc_node_unlock(node
);
175 void tipc_node_remove_conn(u32 dnode
, u32 port
)
177 struct tipc_node
*node
;
178 struct tipc_sock_conn
*conn
, *safe
;
180 if (in_own_node(dnode
))
183 node
= tipc_node_find(dnode
);
187 tipc_node_lock(node
);
188 list_for_each_entry_safe(conn
, safe
, &node
->conn_sks
, list
) {
189 if (port
!= conn
->port
)
191 list_del(&conn
->list
);
194 tipc_node_unlock(node
);
197 void tipc_node_abort_sock_conns(struct list_head
*conns
)
199 struct tipc_sock_conn
*conn
, *safe
;
202 list_for_each_entry_safe(conn
, safe
, conns
, list
) {
203 buf
= tipc_msg_create(TIPC_CRITICAL_IMPORTANCE
, TIPC_CONN_MSG
,
204 SHORT_H_SIZE
, 0, tipc_own_addr
,
205 conn
->peer_node
, conn
->port
,
206 conn
->peer_port
, TIPC_ERR_NO_NODE
);
209 list_del(&conn
->list
);
215 * tipc_node_link_up - handle addition of link
217 * Link becomes active (alone or shared) or standby, depending on its priority.
219 void tipc_node_link_up(struct tipc_node
*n_ptr
, struct tipc_link
*l_ptr
)
221 struct tipc_link
**active
= &n_ptr
->active_links
[0];
222 u32 addr
= n_ptr
->addr
;
224 n_ptr
->working_links
++;
225 tipc_nametbl_publish(TIPC_LINK_STATE
, addr
, addr
, TIPC_NODE_SCOPE
,
226 l_ptr
->bearer_id
, addr
);
227 pr_info("Established link <%s> on network plane %c\n",
228 l_ptr
->name
, l_ptr
->net_plane
);
231 active
[0] = active
[1] = l_ptr
;
232 node_established_contact(n_ptr
);
235 if (l_ptr
->priority
< active
[0]->priority
) {
236 pr_info("New link <%s> becomes standby\n", l_ptr
->name
);
239 tipc_link_dup_queue_xmit(active
[0], l_ptr
);
240 if (l_ptr
->priority
== active
[0]->priority
) {
244 pr_info("Old link <%s> becomes standby\n", active
[0]->name
);
245 if (active
[1] != active
[0])
246 pr_info("Old link <%s> becomes standby\n", active
[1]->name
);
247 active
[0] = active
[1] = l_ptr
;
249 /* Leave room for changeover header when returning 'mtu' to users: */
250 n_ptr
->act_mtus
[0] = active
[0]->max_pkt
- INT_H_SIZE
;
251 n_ptr
->act_mtus
[1] = active
[1]->max_pkt
- INT_H_SIZE
;
255 * node_select_active_links - select active link
257 static void node_select_active_links(struct tipc_node
*n_ptr
)
259 struct tipc_link
**active
= &n_ptr
->active_links
[0];
261 u32 highest_prio
= 0;
263 active
[0] = active
[1] = NULL
;
265 for (i
= 0; i
< MAX_BEARERS
; i
++) {
266 struct tipc_link
*l_ptr
= n_ptr
->links
[i
];
268 if (!l_ptr
|| !tipc_link_is_up(l_ptr
) ||
269 (l_ptr
->priority
< highest_prio
))
272 if (l_ptr
->priority
> highest_prio
) {
273 highest_prio
= l_ptr
->priority
;
274 active
[0] = active
[1] = l_ptr
;
282 * tipc_node_link_down - handle loss of link
284 void tipc_node_link_down(struct tipc_node
*n_ptr
, struct tipc_link
*l_ptr
)
286 struct tipc_link
**active
;
287 u32 addr
= n_ptr
->addr
;
289 n_ptr
->working_links
--;
290 tipc_nametbl_withdraw(TIPC_LINK_STATE
, addr
, l_ptr
->bearer_id
, addr
);
292 if (!tipc_link_is_active(l_ptr
)) {
293 pr_info("Lost standby link <%s> on network plane %c\n",
294 l_ptr
->name
, l_ptr
->net_plane
);
297 pr_info("Lost link <%s> on network plane %c\n",
298 l_ptr
->name
, l_ptr
->net_plane
);
300 active
= &n_ptr
->active_links
[0];
301 if (active
[0] == l_ptr
)
302 active
[0] = active
[1];
303 if (active
[1] == l_ptr
)
304 active
[1] = active
[0];
305 if (active
[0] == l_ptr
)
306 node_select_active_links(n_ptr
);
307 if (tipc_node_is_up(n_ptr
))
308 tipc_link_failover_send_queue(l_ptr
);
310 node_lost_contact(n_ptr
);
312 /* Leave room for changeover header when returning 'mtu' to users: */
314 n_ptr
->act_mtus
[0] = active
[0]->max_pkt
- INT_H_SIZE
;
315 n_ptr
->act_mtus
[1] = active
[1]->max_pkt
- INT_H_SIZE
;
319 /* Loopback link went down? No fragmentation needed from now on. */
320 if (n_ptr
->addr
== tipc_own_addr
) {
321 n_ptr
->act_mtus
[0] = MAX_MSG_SIZE
;
322 n_ptr
->act_mtus
[1] = MAX_MSG_SIZE
;
326 int tipc_node_active_links(struct tipc_node
*n_ptr
)
328 return n_ptr
->active_links
[0] != NULL
;
331 int tipc_node_is_up(struct tipc_node
*n_ptr
)
333 return tipc_node_active_links(n_ptr
);
336 void tipc_node_attach_link(struct tipc_node
*n_ptr
, struct tipc_link
*l_ptr
)
338 n_ptr
->links
[l_ptr
->bearer_id
] = l_ptr
;
339 spin_lock_bh(&node_list_lock
);
341 spin_unlock_bh(&node_list_lock
);
345 void tipc_node_detach_link(struct tipc_node
*n_ptr
, struct tipc_link
*l_ptr
)
349 for (i
= 0; i
< MAX_BEARERS
; i
++) {
350 if (l_ptr
!= n_ptr
->links
[i
])
352 n_ptr
->links
[i
] = NULL
;
353 spin_lock_bh(&node_list_lock
);
355 spin_unlock_bh(&node_list_lock
);
360 static void node_established_contact(struct tipc_node
*n_ptr
)
362 n_ptr
->action_flags
|= TIPC_NOTIFY_NODE_UP
;
363 n_ptr
->bclink
.oos_state
= 0;
364 n_ptr
->bclink
.acked
= tipc_bclink_get_last_sent();
365 tipc_bclink_add_node(n_ptr
->addr
);
368 static void node_lost_contact(struct tipc_node
*n_ptr
)
370 char addr_string
[16];
373 pr_info("Lost contact with %s\n",
374 tipc_addr_string_fill(addr_string
, n_ptr
->addr
));
376 /* Flush broadcast link info associated with lost node */
377 if (n_ptr
->bclink
.recv_permitted
) {
378 kfree_skb_list(n_ptr
->bclink
.deferred_head
);
379 n_ptr
->bclink
.deferred_size
= 0;
381 if (n_ptr
->bclink
.reasm_buf
) {
382 kfree_skb(n_ptr
->bclink
.reasm_buf
);
383 n_ptr
->bclink
.reasm_buf
= NULL
;
386 tipc_bclink_remove_node(n_ptr
->addr
);
387 tipc_bclink_acknowledge(n_ptr
, INVALID_LINK_SEQ
);
389 n_ptr
->bclink
.recv_permitted
= false;
392 /* Abort link changeover */
393 for (i
= 0; i
< MAX_BEARERS
; i
++) {
394 struct tipc_link
*l_ptr
= n_ptr
->links
[i
];
397 l_ptr
->reset_checkpoint
= l_ptr
->next_in_no
;
398 l_ptr
->exp_msg_count
= 0;
399 tipc_link_reset_fragments(l_ptr
);
402 n_ptr
->action_flags
&= ~TIPC_WAIT_OWN_LINKS_DOWN
;
404 /* Notify subscribers and prevent re-contact with node until
407 n_ptr
->action_flags
|= TIPC_WAIT_PEER_LINKS_DOWN
|
408 TIPC_NOTIFY_NODE_DOWN
;
411 struct sk_buff
*tipc_node_get_nodes(const void *req_tlv_area
, int req_tlv_space
)
415 struct tipc_node
*n_ptr
;
416 struct tipc_node_info node_info
;
419 if (!TLV_CHECK(req_tlv_area
, req_tlv_space
, TIPC_TLV_NET_ADDR
))
420 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR
);
422 domain
= ntohl(*(__be32
*)TLV_DATA(req_tlv_area
));
423 if (!tipc_addr_domain_valid(domain
))
424 return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
425 " (network address)");
427 spin_lock_bh(&node_list_lock
);
428 if (!tipc_num_nodes
) {
429 spin_unlock_bh(&node_list_lock
);
430 return tipc_cfg_reply_none();
433 /* For now, get space for all other nodes */
434 payload_size
= TLV_SPACE(sizeof(node_info
)) * tipc_num_nodes
;
435 if (payload_size
> 32768u) {
436 spin_unlock_bh(&node_list_lock
);
437 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
438 " (too many nodes)");
440 spin_unlock_bh(&node_list_lock
);
442 buf
= tipc_cfg_reply_alloc(payload_size
);
446 /* Add TLVs for all nodes in scope */
448 list_for_each_entry_rcu(n_ptr
, &tipc_node_list
, list
) {
449 if (!tipc_in_scope(domain
, n_ptr
->addr
))
451 node_info
.addr
= htonl(n_ptr
->addr
);
452 node_info
.up
= htonl(tipc_node_is_up(n_ptr
));
453 tipc_cfg_append_tlv(buf
, TIPC_TLV_NODE_INFO
,
454 &node_info
, sizeof(node_info
));
460 struct sk_buff
*tipc_node_get_links(const void *req_tlv_area
, int req_tlv_space
)
464 struct tipc_node
*n_ptr
;
465 struct tipc_link_info link_info
;
468 if (!TLV_CHECK(req_tlv_area
, req_tlv_space
, TIPC_TLV_NET_ADDR
))
469 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR
);
471 domain
= ntohl(*(__be32
*)TLV_DATA(req_tlv_area
));
472 if (!tipc_addr_domain_valid(domain
))
473 return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
474 " (network address)");
477 return tipc_cfg_reply_none();
479 spin_lock_bh(&node_list_lock
);
480 /* Get space for all unicast links + broadcast link */
481 payload_size
= TLV_SPACE((sizeof(link_info
)) * (tipc_num_links
+ 1));
482 if (payload_size
> 32768u) {
483 spin_unlock_bh(&node_list_lock
);
484 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
485 " (too many links)");
487 spin_unlock_bh(&node_list_lock
);
489 buf
= tipc_cfg_reply_alloc(payload_size
);
493 /* Add TLV for broadcast link */
494 link_info
.dest
= htonl(tipc_cluster_mask(tipc_own_addr
));
495 link_info
.up
= htonl(1);
496 strlcpy(link_info
.str
, tipc_bclink_name
, TIPC_MAX_LINK_NAME
);
497 tipc_cfg_append_tlv(buf
, TIPC_TLV_LINK_INFO
, &link_info
, sizeof(link_info
));
499 /* Add TLVs for any other links in scope */
501 list_for_each_entry_rcu(n_ptr
, &tipc_node_list
, list
) {
504 if (!tipc_in_scope(domain
, n_ptr
->addr
))
506 tipc_node_lock(n_ptr
);
507 for (i
= 0; i
< MAX_BEARERS
; i
++) {
508 if (!n_ptr
->links
[i
])
510 link_info
.dest
= htonl(n_ptr
->addr
);
511 link_info
.up
= htonl(tipc_link_is_up(n_ptr
->links
[i
]));
512 strcpy(link_info
.str
, n_ptr
->links
[i
]->name
);
513 tipc_cfg_append_tlv(buf
, TIPC_TLV_LINK_INFO
,
514 &link_info
, sizeof(link_info
));
516 tipc_node_unlock(n_ptr
);
523 * tipc_node_get_linkname - get the name of a link
525 * @bearer_id: id of the bearer
526 * @node: peer node address
527 * @linkname: link name output buffer
529 * Returns 0 on success
531 int tipc_node_get_linkname(u32 bearer_id
, u32 addr
, char *linkname
, size_t len
)
533 struct tipc_link
*link
;
534 struct tipc_node
*node
= tipc_node_find(addr
);
536 if ((bearer_id
>= MAX_BEARERS
) || !node
)
538 tipc_node_lock(node
);
539 link
= node
->links
[bearer_id
];
541 strncpy(linkname
, link
->name
, len
);
542 tipc_node_unlock(node
);
545 tipc_node_unlock(node
);
549 void tipc_node_unlock(struct tipc_node
*node
)
551 LIST_HEAD(nsub_list
);
553 struct sk_buff_head waiting_sks
;
555 unsigned int flags
= node
->action_flags
;
557 if (likely(!node
->action_flags
)) {
558 spin_unlock_bh(&node
->lock
);
562 __skb_queue_head_init(&waiting_sks
);
563 if (node
->action_flags
& TIPC_WAKEUP_USERS
) {
564 skb_queue_splice_init(&node
->waiting_sks
, &waiting_sks
);
565 node
->action_flags
&= ~TIPC_WAKEUP_USERS
;
567 if (node
->action_flags
& TIPC_NOTIFY_NODE_DOWN
) {
568 list_replace_init(&node
->nsub
, &nsub_list
);
569 list_replace_init(&node
->conn_sks
, &conn_sks
);
570 node
->action_flags
&= ~TIPC_NOTIFY_NODE_DOWN
;
572 if (node
->action_flags
& TIPC_NOTIFY_NODE_UP
) {
573 node
->action_flags
&= ~TIPC_NOTIFY_NODE_UP
;
576 node
->action_flags
&= ~TIPC_WAKEUP_BCAST_USERS
;
577 spin_unlock_bh(&node
->lock
);
579 while (!skb_queue_empty(&waiting_sks
))
580 tipc_sk_rcv(__skb_dequeue(&waiting_sks
));
582 if (!list_empty(&conn_sks
))
583 tipc_node_abort_sock_conns(&conn_sks
);
585 if (!list_empty(&nsub_list
))
586 tipc_nodesub_notify(&nsub_list
);
588 if (flags
& TIPC_WAKEUP_BCAST_USERS
)
589 tipc_bclink_wakeup_users();
592 tipc_named_node_up(addr
);