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
);
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).
60 static unsigned int tipc_hashfn(u32 addr
)
62 return addr
& (NODE_HTABLE_SIZE
- 1);
66 * tipc_node_find - locate specified node object, if it exists
68 struct tipc_node
*tipc_node_find(u32 addr
)
70 struct tipc_node
*node
;
72 if (unlikely(!in_own_cluster_exact(addr
)))
76 hlist_for_each_entry_rcu(node
, &node_htable
[tipc_hashfn(addr
)], hash
) {
77 if (node
->addr
== addr
) {
86 struct tipc_node
*tipc_node_create(u32 addr
)
88 struct tipc_node
*n_ptr
, *temp_node
;
90 spin_lock_bh(&node_list_lock
);
92 n_ptr
= kzalloc(sizeof(*n_ptr
), GFP_ATOMIC
);
94 spin_unlock_bh(&node_list_lock
);
95 pr_warn("Node creation failed, no memory\n");
100 spin_lock_init(&n_ptr
->lock
);
101 INIT_HLIST_NODE(&n_ptr
->hash
);
102 INIT_LIST_HEAD(&n_ptr
->list
);
103 INIT_LIST_HEAD(&n_ptr
->nsub
);
104 __skb_queue_head_init(&n_ptr
->waiting_sks
);
106 hlist_add_head_rcu(&n_ptr
->hash
, &node_htable
[tipc_hashfn(addr
)]);
108 list_for_each_entry_rcu(temp_node
, &tipc_node_list
, list
) {
109 if (n_ptr
->addr
< temp_node
->addr
)
112 list_add_tail_rcu(&n_ptr
->list
, &temp_node
->list
);
113 n_ptr
->action_flags
= TIPC_WAIT_PEER_LINKS_DOWN
;
114 n_ptr
->signature
= INVALID_NODE_SIG
;
118 spin_unlock_bh(&node_list_lock
);
122 static void tipc_node_delete(struct tipc_node
*n_ptr
)
124 list_del_rcu(&n_ptr
->list
);
125 hlist_del_rcu(&n_ptr
->hash
);
126 kfree_rcu(n_ptr
, rcu
);
131 void tipc_node_stop(void)
133 struct tipc_node
*node
, *t_node
;
135 spin_lock_bh(&node_list_lock
);
136 list_for_each_entry_safe(node
, t_node
, &tipc_node_list
, list
)
137 tipc_node_delete(node
);
138 spin_unlock_bh(&node_list_lock
);
142 * tipc_node_link_up - handle addition of link
144 * Link becomes active (alone or shared) or standby, depending on its priority.
146 void tipc_node_link_up(struct tipc_node
*n_ptr
, struct tipc_link
*l_ptr
)
148 struct tipc_link
**active
= &n_ptr
->active_links
[0];
149 u32 addr
= n_ptr
->addr
;
151 n_ptr
->working_links
++;
152 tipc_nametbl_publish(TIPC_LINK_STATE
, addr
, addr
, TIPC_NODE_SCOPE
,
153 l_ptr
->bearer_id
, addr
);
154 pr_info("Established link <%s> on network plane %c\n",
155 l_ptr
->name
, l_ptr
->net_plane
);
158 active
[0] = active
[1] = l_ptr
;
159 node_established_contact(n_ptr
);
162 if (l_ptr
->priority
< active
[0]->priority
) {
163 pr_info("New link <%s> becomes standby\n", l_ptr
->name
);
166 tipc_link_dup_queue_xmit(active
[0], l_ptr
);
167 if (l_ptr
->priority
== active
[0]->priority
) {
171 pr_info("Old link <%s> becomes standby\n", active
[0]->name
);
172 if (active
[1] != active
[0])
173 pr_info("Old link <%s> becomes standby\n", active
[1]->name
);
174 active
[0] = active
[1] = l_ptr
;
176 /* Leave room for changeover header when returning 'mtu' to users: */
177 n_ptr
->act_mtus
[0] = active
[0]->max_pkt
- INT_H_SIZE
;
178 n_ptr
->act_mtus
[1] = active
[1]->max_pkt
- INT_H_SIZE
;
182 * node_select_active_links - select active link
184 static void node_select_active_links(struct tipc_node
*n_ptr
)
186 struct tipc_link
**active
= &n_ptr
->active_links
[0];
188 u32 highest_prio
= 0;
190 active
[0] = active
[1] = NULL
;
192 for (i
= 0; i
< MAX_BEARERS
; i
++) {
193 struct tipc_link
*l_ptr
= n_ptr
->links
[i
];
195 if (!l_ptr
|| !tipc_link_is_up(l_ptr
) ||
196 (l_ptr
->priority
< highest_prio
))
199 if (l_ptr
->priority
> highest_prio
) {
200 highest_prio
= l_ptr
->priority
;
201 active
[0] = active
[1] = l_ptr
;
209 * tipc_node_link_down - handle loss of link
211 void tipc_node_link_down(struct tipc_node
*n_ptr
, struct tipc_link
*l_ptr
)
213 struct tipc_link
**active
;
214 u32 addr
= n_ptr
->addr
;
216 n_ptr
->working_links
--;
217 tipc_nametbl_withdraw(TIPC_LINK_STATE
, addr
, l_ptr
->bearer_id
, addr
);
219 if (!tipc_link_is_active(l_ptr
)) {
220 pr_info("Lost standby link <%s> on network plane %c\n",
221 l_ptr
->name
, l_ptr
->net_plane
);
224 pr_info("Lost link <%s> on network plane %c\n",
225 l_ptr
->name
, l_ptr
->net_plane
);
227 active
= &n_ptr
->active_links
[0];
228 if (active
[0] == l_ptr
)
229 active
[0] = active
[1];
230 if (active
[1] == l_ptr
)
231 active
[1] = active
[0];
232 if (active
[0] == l_ptr
)
233 node_select_active_links(n_ptr
);
234 if (tipc_node_is_up(n_ptr
))
235 tipc_link_failover_send_queue(l_ptr
);
237 node_lost_contact(n_ptr
);
239 /* Leave room for changeover header when returning 'mtu' to users: */
241 n_ptr
->act_mtus
[0] = active
[0]->max_pkt
- INT_H_SIZE
;
242 n_ptr
->act_mtus
[1] = active
[1]->max_pkt
- INT_H_SIZE
;
246 /* Loopback link went down? No fragmentation needed from now on. */
247 if (n_ptr
->addr
== tipc_own_addr
) {
248 n_ptr
->act_mtus
[0] = MAX_MSG_SIZE
;
249 n_ptr
->act_mtus
[1] = MAX_MSG_SIZE
;
253 int tipc_node_active_links(struct tipc_node
*n_ptr
)
255 return n_ptr
->active_links
[0] != NULL
;
258 int tipc_node_is_up(struct tipc_node
*n_ptr
)
260 return tipc_node_active_links(n_ptr
);
263 void tipc_node_attach_link(struct tipc_node
*n_ptr
, struct tipc_link
*l_ptr
)
265 n_ptr
->links
[l_ptr
->bearer_id
] = l_ptr
;
266 spin_lock_bh(&node_list_lock
);
268 spin_unlock_bh(&node_list_lock
);
272 void tipc_node_detach_link(struct tipc_node
*n_ptr
, struct tipc_link
*l_ptr
)
276 for (i
= 0; i
< MAX_BEARERS
; i
++) {
277 if (l_ptr
!= n_ptr
->links
[i
])
279 n_ptr
->links
[i
] = NULL
;
280 spin_lock_bh(&node_list_lock
);
282 spin_unlock_bh(&node_list_lock
);
287 static void node_established_contact(struct tipc_node
*n_ptr
)
289 n_ptr
->action_flags
|= TIPC_NOTIFY_NODE_UP
;
290 n_ptr
->bclink
.oos_state
= 0;
291 n_ptr
->bclink
.acked
= tipc_bclink_get_last_sent();
292 tipc_bclink_add_node(n_ptr
->addr
);
295 static void node_lost_contact(struct tipc_node
*n_ptr
)
297 char addr_string
[16];
300 pr_info("Lost contact with %s\n",
301 tipc_addr_string_fill(addr_string
, n_ptr
->addr
));
303 /* Flush broadcast link info associated with lost node */
304 if (n_ptr
->bclink
.recv_permitted
) {
305 kfree_skb_list(n_ptr
->bclink
.deferred_head
);
306 n_ptr
->bclink
.deferred_size
= 0;
308 if (n_ptr
->bclink
.reasm_buf
) {
309 kfree_skb(n_ptr
->bclink
.reasm_buf
);
310 n_ptr
->bclink
.reasm_buf
= NULL
;
313 tipc_bclink_remove_node(n_ptr
->addr
);
314 tipc_bclink_acknowledge(n_ptr
, INVALID_LINK_SEQ
);
316 n_ptr
->bclink
.recv_permitted
= false;
319 /* Abort link changeover */
320 for (i
= 0; i
< MAX_BEARERS
; i
++) {
321 struct tipc_link
*l_ptr
= n_ptr
->links
[i
];
324 l_ptr
->reset_checkpoint
= l_ptr
->next_in_no
;
325 l_ptr
->exp_msg_count
= 0;
326 tipc_link_reset_fragments(l_ptr
);
329 n_ptr
->action_flags
&= ~TIPC_WAIT_OWN_LINKS_DOWN
;
331 /* Notify subscribers and prevent re-contact with node until
334 n_ptr
->action_flags
|= TIPC_WAIT_PEER_LINKS_DOWN
|
335 TIPC_NOTIFY_NODE_DOWN
;
338 struct sk_buff
*tipc_node_get_nodes(const void *req_tlv_area
, int req_tlv_space
)
342 struct tipc_node
*n_ptr
;
343 struct tipc_node_info node_info
;
346 if (!TLV_CHECK(req_tlv_area
, req_tlv_space
, TIPC_TLV_NET_ADDR
))
347 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR
);
349 domain
= ntohl(*(__be32
*)TLV_DATA(req_tlv_area
));
350 if (!tipc_addr_domain_valid(domain
))
351 return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
352 " (network address)");
354 spin_lock_bh(&node_list_lock
);
355 if (!tipc_num_nodes
) {
356 spin_unlock_bh(&node_list_lock
);
357 return tipc_cfg_reply_none();
360 /* For now, get space for all other nodes */
361 payload_size
= TLV_SPACE(sizeof(node_info
)) * tipc_num_nodes
;
362 if (payload_size
> 32768u) {
363 spin_unlock_bh(&node_list_lock
);
364 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
365 " (too many nodes)");
367 spin_unlock_bh(&node_list_lock
);
369 buf
= tipc_cfg_reply_alloc(payload_size
);
373 /* Add TLVs for all nodes in scope */
375 list_for_each_entry_rcu(n_ptr
, &tipc_node_list
, list
) {
376 if (!tipc_in_scope(domain
, n_ptr
->addr
))
378 node_info
.addr
= htonl(n_ptr
->addr
);
379 node_info
.up
= htonl(tipc_node_is_up(n_ptr
));
380 tipc_cfg_append_tlv(buf
, TIPC_TLV_NODE_INFO
,
381 &node_info
, sizeof(node_info
));
387 struct sk_buff
*tipc_node_get_links(const void *req_tlv_area
, int req_tlv_space
)
391 struct tipc_node
*n_ptr
;
392 struct tipc_link_info link_info
;
395 if (!TLV_CHECK(req_tlv_area
, req_tlv_space
, TIPC_TLV_NET_ADDR
))
396 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR
);
398 domain
= ntohl(*(__be32
*)TLV_DATA(req_tlv_area
));
399 if (!tipc_addr_domain_valid(domain
))
400 return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
401 " (network address)");
404 return tipc_cfg_reply_none();
406 spin_lock_bh(&node_list_lock
);
407 /* Get space for all unicast links + broadcast link */
408 payload_size
= TLV_SPACE((sizeof(link_info
)) * (tipc_num_links
+ 1));
409 if (payload_size
> 32768u) {
410 spin_unlock_bh(&node_list_lock
);
411 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
412 " (too many links)");
414 spin_unlock_bh(&node_list_lock
);
416 buf
= tipc_cfg_reply_alloc(payload_size
);
420 /* Add TLV for broadcast link */
421 link_info
.dest
= htonl(tipc_cluster_mask(tipc_own_addr
));
422 link_info
.up
= htonl(1);
423 strlcpy(link_info
.str
, tipc_bclink_name
, TIPC_MAX_LINK_NAME
);
424 tipc_cfg_append_tlv(buf
, TIPC_TLV_LINK_INFO
, &link_info
, sizeof(link_info
));
426 /* Add TLVs for any other links in scope */
428 list_for_each_entry_rcu(n_ptr
, &tipc_node_list
, list
) {
431 if (!tipc_in_scope(domain
, n_ptr
->addr
))
433 tipc_node_lock(n_ptr
);
434 for (i
= 0; i
< MAX_BEARERS
; i
++) {
435 if (!n_ptr
->links
[i
])
437 link_info
.dest
= htonl(n_ptr
->addr
);
438 link_info
.up
= htonl(tipc_link_is_up(n_ptr
->links
[i
]));
439 strcpy(link_info
.str
, n_ptr
->links
[i
]->name
);
440 tipc_cfg_append_tlv(buf
, TIPC_TLV_LINK_INFO
,
441 &link_info
, sizeof(link_info
));
443 tipc_node_unlock(n_ptr
);
450 * tipc_node_get_linkname - get the name of a link
452 * @bearer_id: id of the bearer
453 * @node: peer node address
454 * @linkname: link name output buffer
456 * Returns 0 on success
458 int tipc_node_get_linkname(u32 bearer_id
, u32 addr
, char *linkname
, size_t len
)
460 struct tipc_link
*link
;
461 struct tipc_node
*node
= tipc_node_find(addr
);
463 if ((bearer_id
>= MAX_BEARERS
) || !node
)
465 tipc_node_lock(node
);
466 link
= node
->links
[bearer_id
];
468 strncpy(linkname
, link
->name
, len
);
469 tipc_node_unlock(node
);
472 tipc_node_unlock(node
);
476 void tipc_node_unlock(struct tipc_node
*node
)
478 LIST_HEAD(nsub_list
);
479 struct sk_buff_head waiting_sks
;
482 if (likely(!node
->action_flags
)) {
483 spin_unlock_bh(&node
->lock
);
487 __skb_queue_head_init(&waiting_sks
);
488 if (node
->action_flags
& TIPC_WAKEUP_USERS
) {
489 skb_queue_splice_init(&node
->waiting_sks
, &waiting_sks
);
490 node
->action_flags
&= ~TIPC_WAKEUP_USERS
;
492 if (node
->action_flags
& TIPC_NOTIFY_NODE_DOWN
) {
493 list_replace_init(&node
->nsub
, &nsub_list
);
494 node
->action_flags
&= ~TIPC_NOTIFY_NODE_DOWN
;
496 if (node
->action_flags
& TIPC_NOTIFY_NODE_UP
) {
497 node
->action_flags
&= ~TIPC_NOTIFY_NODE_UP
;
500 spin_unlock_bh(&node
->lock
);
502 while (!skb_queue_empty(&waiting_sks
))
503 tipc_sk_rcv(__skb_dequeue(&waiting_sks
));
505 if (!list_empty(&nsub_list
))
506 tipc_nodesub_notify(&nsub_list
);
509 tipc_named_node_up(addr
);