2 * net/tipc/node.c: TIPC node management routines
4 * Copyright (c) 2000-2006, Ericsson AB
5 * Copyright (c) 2005-2006, 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.
43 #include "node_subscr.h"
47 #include "name_distr.h"
49 void node_print(struct print_buf
*buf
, struct node
*n_ptr
, char *str
);
50 static void node_lost_contact(struct node
*n_ptr
);
51 static void node_established_contact(struct node
*n_ptr
);
53 struct node
*tipc_nodes
= NULL
; /* sorted list of nodes within cluster */
57 struct node
*tipc_node_create(u32 addr
)
59 struct cluster
*c_ptr
;
61 struct node
**curr_node
;
63 n_ptr
= kzalloc(sizeof(*n_ptr
),GFP_ATOMIC
);
65 warn("Node creation failed, no memory\n");
69 c_ptr
= tipc_cltr_find(addr
);
71 c_ptr
= tipc_cltr_create(addr
);
79 spin_lock_init(&n_ptr
->lock
);
80 INIT_LIST_HEAD(&n_ptr
->nsub
);
82 tipc_cltr_attach_node(c_ptr
, n_ptr
);
83 n_ptr
->last_router
= -1;
85 /* Insert node into ordered list */
86 for (curr_node
= &tipc_nodes
; *curr_node
;
87 curr_node
= &(*curr_node
)->next
) {
88 if (addr
< (*curr_node
)->addr
) {
89 n_ptr
->next
= *curr_node
;
97 void tipc_node_delete(struct node
*n_ptr
)
103 /* Not needed because links are already deleted via tipc_bearer_stop() */
107 for (l_num
= 0; l_num
< MAX_BEARERS
; l_num
++) {
108 link_delete(n_ptr
->links
[l_num
]);
112 dbg("node %x deleted\n", n_ptr
->addr
);
118 * tipc_node_link_up - handle addition of link
120 * Link becomes active (alone or shared) or standby, depending on its priority.
123 void tipc_node_link_up(struct node
*n_ptr
, struct link
*l_ptr
)
125 struct link
**active
= &n_ptr
->active_links
[0];
127 n_ptr
->working_links
++;
129 info("Established link <%s> on network plane %c\n",
130 l_ptr
->name
, l_ptr
->b_ptr
->net_plane
);
133 dbg(" link %x into %x/%x\n", l_ptr
, &active
[0], &active
[1]);
134 active
[0] = active
[1] = l_ptr
;
135 node_established_contact(n_ptr
);
138 if (l_ptr
->priority
< active
[0]->priority
) {
139 info("New link <%s> becomes standby\n", l_ptr
->name
);
142 tipc_link_send_duplicate(active
[0], l_ptr
);
143 if (l_ptr
->priority
== active
[0]->priority
) {
147 info("Old link <%s> becomes standby\n", active
[0]->name
);
148 if (active
[1] != active
[0])
149 info("Old link <%s> becomes standby\n", active
[1]->name
);
150 active
[0] = active
[1] = l_ptr
;
154 * node_select_active_links - select active link
157 static void node_select_active_links(struct node
*n_ptr
)
159 struct link
**active
= &n_ptr
->active_links
[0];
161 u32 highest_prio
= 0;
163 active
[0] = active
[1] = NULL
;
165 for (i
= 0; i
< MAX_BEARERS
; i
++) {
166 struct link
*l_ptr
= n_ptr
->links
[i
];
168 if (!l_ptr
|| !tipc_link_is_up(l_ptr
) ||
169 (l_ptr
->priority
< highest_prio
))
172 if (l_ptr
->priority
> highest_prio
) {
173 highest_prio
= l_ptr
->priority
;
174 active
[0] = active
[1] = l_ptr
;
182 * tipc_node_link_down - handle loss of link
185 void tipc_node_link_down(struct node
*n_ptr
, struct link
*l_ptr
)
187 struct link
**active
;
189 n_ptr
->working_links
--;
191 if (!tipc_link_is_active(l_ptr
)) {
192 info("Lost standby link <%s> on network plane %c\n",
193 l_ptr
->name
, l_ptr
->b_ptr
->net_plane
);
196 info("Lost link <%s> on network plane %c\n",
197 l_ptr
->name
, l_ptr
->b_ptr
->net_plane
);
199 active
= &n_ptr
->active_links
[0];
200 if (active
[0] == l_ptr
)
201 active
[0] = active
[1];
202 if (active
[1] == l_ptr
)
203 active
[1] = active
[0];
204 if (active
[0] == l_ptr
)
205 node_select_active_links(n_ptr
);
206 if (tipc_node_is_up(n_ptr
))
207 tipc_link_changeover(l_ptr
);
209 node_lost_contact(n_ptr
);
212 int tipc_node_has_active_links(struct node
*n_ptr
)
215 ((n_ptr
->active_links
[0]) || (n_ptr
->active_links
[1])));
218 int tipc_node_has_redundant_links(struct node
*n_ptr
)
220 return (n_ptr
->working_links
> 1);
223 static int tipc_node_has_active_routes(struct node
*n_ptr
)
225 return (n_ptr
&& (n_ptr
->last_router
>= 0));
228 int tipc_node_is_up(struct node
*n_ptr
)
230 return (tipc_node_has_active_links(n_ptr
) || tipc_node_has_active_routes(n_ptr
));
233 struct node
*tipc_node_attach_link(struct link
*l_ptr
)
235 struct node
*n_ptr
= tipc_node_find(l_ptr
->addr
);
238 n_ptr
= tipc_node_create(l_ptr
->addr
);
240 u32 bearer_id
= l_ptr
->b_ptr
->identity
;
241 char addr_string
[16];
243 if (n_ptr
->link_cnt
>= 2) {
244 err("Attempt to create third link to %s\n",
245 addr_string_fill(addr_string
, n_ptr
->addr
));
249 if (!n_ptr
->links
[bearer_id
]) {
250 n_ptr
->links
[bearer_id
] = l_ptr
;
251 tipc_net
.zones
[tipc_zone(l_ptr
->addr
)]->links
++;
255 err("Attempt to establish second link on <%s> to %s \n",
256 l_ptr
->b_ptr
->publ
.name
,
257 addr_string_fill(addr_string
, l_ptr
->addr
));
262 void tipc_node_detach_link(struct node
*n_ptr
, struct link
*l_ptr
)
264 n_ptr
->links
[l_ptr
->b_ptr
->identity
] = NULL
;
265 tipc_net
.zones
[tipc_zone(l_ptr
->addr
)]->links
--;
270 * Routing table management - five cases to handle:
272 * 1: A link towards a zone/cluster external node comes up.
273 * => Send a multicast message updating routing tables of all
274 * system nodes within own cluster that the new destination
275 * can be reached via this node.
276 * (node.establishedContact()=>cluster.multicastNewRoute())
278 * 2: A link towards a slave node comes up.
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.
282 * (node.establishedContact()=>cluster.multicastNewRoute())
283 * => Send a message to the slave node about existence
284 * of all system nodes within cluster:
285 * (node.establishedContact()=>cluster.sendLocalRoutes())
287 * 3: A new cluster local system node becomes available.
288 * => Send message(s) to this particular node containing
289 * information about all cluster external and slave
290 * nodes which can be reached via this node.
291 * (node.establishedContact()==>network.sendExternalRoutes())
292 * (node.establishedContact()==>network.sendSlaveRoutes())
293 * => Send messages to all directly connected slave nodes
294 * containing information about the existence of the new node
295 * (node.establishedContact()=>cluster.multicastNewRoute())
297 * 4: The link towards a zone/cluster external node or slave
299 * => Send a multcast message updating routing tables of all
300 * nodes within cluster that the new destination can not any
301 * longer be reached via this node.
302 * (node.lostAllLinks()=>cluster.bcastLostRoute())
304 * 5: A cluster local system node becomes unavailable.
305 * => Remove all references to this node from the local
306 * routing tables. Note: This is a completely node
308 * (node.lostAllLinks()=>network.removeAsRouter())
309 * => Send messages to all directly connected slave nodes
310 * containing information about loss of the node
311 * (node.establishedContact()=>cluster.multicastLostRoute())
315 static void node_established_contact(struct node
*n_ptr
)
317 struct cluster
*c_ptr
;
319 dbg("node_established_contact:-> %x\n", n_ptr
->addr
);
320 if (!tipc_node_has_active_routes(n_ptr
) && in_own_cluster(n_ptr
->addr
)) {
321 tipc_k_signal((Handler
)tipc_named_node_up
, n_ptr
->addr
);
324 /* Syncronize broadcast acks */
325 n_ptr
->bclink
.acked
= tipc_bclink_get_last_sent();
327 if (is_slave(tipc_own_addr
))
329 if (!in_own_cluster(n_ptr
->addr
)) {
330 /* Usage case 1 (see above) */
331 c_ptr
= tipc_cltr_find(tipc_own_addr
);
333 c_ptr
= tipc_cltr_create(tipc_own_addr
);
335 tipc_cltr_bcast_new_route(c_ptr
, n_ptr
->addr
, 1,
340 c_ptr
= n_ptr
->owner
;
341 if (is_slave(n_ptr
->addr
)) {
342 /* Usage case 2 (see above) */
343 tipc_cltr_bcast_new_route(c_ptr
, n_ptr
->addr
, 1, tipc_max_nodes
);
344 tipc_cltr_send_local_routes(c_ptr
, n_ptr
->addr
);
348 if (n_ptr
->bclink
.supported
) {
349 tipc_nmap_add(&tipc_cltr_bcast_nodes
, n_ptr
->addr
);
350 if (n_ptr
->addr
< tipc_own_addr
)
354 /* Case 3 (see above) */
355 tipc_net_send_external_routes(n_ptr
->addr
);
356 tipc_cltr_send_slave_routes(c_ptr
, n_ptr
->addr
);
357 tipc_cltr_bcast_new_route(c_ptr
, n_ptr
->addr
, LOWEST_SLAVE
,
358 tipc_highest_allowed_slave
);
361 static void node_lost_contact(struct node
*n_ptr
)
363 struct cluster
*c_ptr
;
364 struct node_subscr
*ns
, *tns
;
365 char addr_string
[16];
368 /* Clean up broadcast reception remains */
369 n_ptr
->bclink
.gap_after
= n_ptr
->bclink
.gap_to
= 0;
370 while (n_ptr
->bclink
.deferred_head
) {
371 struct sk_buff
* buf
= n_ptr
->bclink
.deferred_head
;
372 n_ptr
->bclink
.deferred_head
= buf
->next
;
375 if (n_ptr
->bclink
.defragm
) {
376 buf_discard(n_ptr
->bclink
.defragm
);
377 n_ptr
->bclink
.defragm
= NULL
;
379 if (in_own_cluster(n_ptr
->addr
) && n_ptr
->bclink
.supported
) {
380 tipc_bclink_acknowledge(n_ptr
, mod(n_ptr
->bclink
.acked
+ 10000));
383 /* Update routing tables */
384 if (is_slave(tipc_own_addr
)) {
385 tipc_net_remove_as_router(n_ptr
->addr
);
387 if (!in_own_cluster(n_ptr
->addr
)) {
388 /* Case 4 (see above) */
389 c_ptr
= tipc_cltr_find(tipc_own_addr
);
390 tipc_cltr_bcast_lost_route(c_ptr
, n_ptr
->addr
, 1,
393 /* Case 5 (see above) */
394 c_ptr
= tipc_cltr_find(n_ptr
->addr
);
395 if (is_slave(n_ptr
->addr
)) {
396 tipc_cltr_bcast_lost_route(c_ptr
, n_ptr
->addr
, 1,
399 if (n_ptr
->bclink
.supported
) {
400 tipc_nmap_remove(&tipc_cltr_bcast_nodes
,
402 if (n_ptr
->addr
< tipc_own_addr
)
405 tipc_net_remove_as_router(n_ptr
->addr
);
406 tipc_cltr_bcast_lost_route(c_ptr
, n_ptr
->addr
,
408 tipc_highest_allowed_slave
);
412 if (tipc_node_has_active_routes(n_ptr
))
415 info("Lost contact with %s\n",
416 addr_string_fill(addr_string
, n_ptr
->addr
));
418 /* Abort link changeover */
419 for (i
= 0; i
< MAX_BEARERS
; i
++) {
420 struct link
*l_ptr
= n_ptr
->links
[i
];
423 l_ptr
->reset_checkpoint
= l_ptr
->next_in_no
;
424 l_ptr
->exp_msg_count
= 0;
425 tipc_link_reset_fragments(l_ptr
);
428 /* Notify subscribers */
429 list_for_each_entry_safe(ns
, tns
, &n_ptr
->nsub
, nodesub_list
) {
431 list_del_init(&ns
->nodesub_list
);
432 tipc_k_signal((Handler
)ns
->handle_node_down
,
433 (unsigned long)ns
->usr_handle
);
438 * tipc_node_select_next_hop - find the next-hop node for a message
440 * Called by when cluster local lookup has failed.
443 struct node
*tipc_node_select_next_hop(u32 addr
, u32 selector
)
448 if (!tipc_addr_domain_valid(addr
))
451 /* Look for direct link to destination processsor */
452 n_ptr
= tipc_node_find(addr
);
453 if (n_ptr
&& tipc_node_has_active_links(n_ptr
))
456 /* Cluster local system nodes *must* have direct links */
457 if (!is_slave(addr
) && in_own_cluster(addr
))
460 /* Look for cluster local router with direct link to node */
461 router_addr
= tipc_node_select_router(n_ptr
, selector
);
463 return tipc_node_select(router_addr
, selector
);
465 /* Slave nodes can only be accessed within own cluster via a
466 known router with direct link -- if no router was found,give up */
470 /* Inter zone/cluster -- find any direct link to remote cluster */
471 addr
= tipc_addr(tipc_zone(addr
), tipc_cluster(addr
), 0);
472 n_ptr
= tipc_net_select_remote_node(addr
, selector
);
473 if (n_ptr
&& tipc_node_has_active_links(n_ptr
))
476 /* Last resort -- look for any router to anywhere in remote zone */
477 router_addr
= tipc_net_select_router(addr
, selector
);
479 return tipc_node_select(router_addr
, selector
);
485 * tipc_node_select_router - select router to reach specified node
487 * Uses a deterministic and fair algorithm for selecting router node.
490 u32
tipc_node_select_router(struct node
*n_ptr
, u32 ref
)
500 if (n_ptr
->last_router
< 0)
502 ulim
= ((n_ptr
->last_router
+ 1) * 32) - 1;
504 /* Start entry must be random */
505 mask
= tipc_max_nodes
;
511 /* Lookup upwards with wrap-around */
513 if (((n_ptr
->routers
[r
/ 32]) >> (r
% 32)) & 1)
515 } while (++r
<= ulim
);
519 if (((n_ptr
->routers
[r
/ 32]) >> (r
% 32)) & 1)
521 } while (++r
< start
);
524 assert(r
&& (r
<= ulim
));
525 return tipc_addr(own_zone(), own_cluster(), r
);
528 void tipc_node_add_router(struct node
*n_ptr
, u32 router
)
530 u32 r_num
= tipc_node(router
);
532 n_ptr
->routers
[r_num
/ 32] =
533 ((1 << (r_num
% 32)) | n_ptr
->routers
[r_num
/ 32]);
534 n_ptr
->last_router
= tipc_max_nodes
/ 32;
535 while ((--n_ptr
->last_router
>= 0) &&
536 !n_ptr
->routers
[n_ptr
->last_router
]);
539 void tipc_node_remove_router(struct node
*n_ptr
, u32 router
)
541 u32 r_num
= tipc_node(router
);
543 if (n_ptr
->last_router
< 0)
544 return; /* No routes */
546 n_ptr
->routers
[r_num
/ 32] =
547 ((~(1 << (r_num
% 32))) & (n_ptr
->routers
[r_num
/ 32]));
548 n_ptr
->last_router
= tipc_max_nodes
/ 32;
549 while ((--n_ptr
->last_router
>= 0) &&
550 !n_ptr
->routers
[n_ptr
->last_router
]);
552 if (!tipc_node_is_up(n_ptr
))
553 node_lost_contact(n_ptr
);
557 void node_print(struct print_buf
*buf
, struct node
*n_ptr
, char *str
)
561 tipc_printf(buf
, "\n\n%s", str
);
562 for (i
= 0; i
< MAX_BEARERS
; i
++) {
563 if (!n_ptr
->links
[i
])
565 tipc_printf(buf
, "Links[%u]: %x, ", i
, n_ptr
->links
[i
]);
567 tipc_printf(buf
, "Active links: [%x,%x]\n",
568 n_ptr
->active_links
[0], n_ptr
->active_links
[1]);
572 u32
tipc_available_nodes(const u32 domain
)
577 for (n_ptr
= tipc_nodes
; n_ptr
; n_ptr
= n_ptr
->next
) {
578 if (!in_scope(domain
, n_ptr
->addr
))
580 if (tipc_node_is_up(n_ptr
))
586 struct sk_buff
*tipc_node_get_nodes(const void *req_tlv_area
, int req_tlv_space
)
591 struct tipc_node_info node_info
;
594 if (!TLV_CHECK(req_tlv_area
, req_tlv_space
, TIPC_TLV_NET_ADDR
))
595 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR
);
597 domain
= ntohl(*(__be32
*)TLV_DATA(req_tlv_area
));
598 if (!tipc_addr_domain_valid(domain
))
599 return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
600 " (network address)");
603 return tipc_cfg_reply_none();
605 /* For now, get space for all other nodes
606 (will need to modify this when slave nodes are supported */
608 payload_size
= TLV_SPACE(sizeof(node_info
)) * (tipc_max_nodes
- 1);
609 if (payload_size
> 32768u)
610 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
611 " (too many nodes)");
612 buf
= tipc_cfg_reply_alloc(payload_size
);
616 /* Add TLVs for all nodes in scope */
618 for (n_ptr
= tipc_nodes
; n_ptr
; n_ptr
= n_ptr
->next
) {
619 if (!in_scope(domain
, n_ptr
->addr
))
621 node_info
.addr
= htonl(n_ptr
->addr
);
622 node_info
.up
= htonl(tipc_node_is_up(n_ptr
));
623 tipc_cfg_append_tlv(buf
, TIPC_TLV_NODE_INFO
,
624 &node_info
, sizeof(node_info
));
630 struct sk_buff
*tipc_node_get_links(const void *req_tlv_area
, int req_tlv_space
)
635 struct tipc_link_info link_info
;
638 if (!TLV_CHECK(req_tlv_area
, req_tlv_space
, TIPC_TLV_NET_ADDR
))
639 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR
);
641 domain
= ntohl(*(__be32
*)TLV_DATA(req_tlv_area
));
642 if (!tipc_addr_domain_valid(domain
))
643 return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
644 " (network address)");
646 if (tipc_mode
!= TIPC_NET_MODE
)
647 return tipc_cfg_reply_none();
649 /* Get space for all unicast links + multicast link */
651 payload_size
= TLV_SPACE(sizeof(link_info
)) *
652 (tipc_net
.zones
[tipc_zone(tipc_own_addr
)]->links
+ 1);
653 if (payload_size
> 32768u)
654 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
655 " (too many links)");
656 buf
= tipc_cfg_reply_alloc(payload_size
);
660 /* Add TLV for broadcast link */
662 link_info
.dest
= htonl(tipc_own_addr
& 0xfffff00);
663 link_info
.up
= htonl(1);
664 sprintf(link_info
.str
, tipc_bclink_name
);
665 tipc_cfg_append_tlv(buf
, TIPC_TLV_LINK_INFO
, &link_info
, sizeof(link_info
));
667 /* Add TLVs for any other links in scope */
669 for (n_ptr
= tipc_nodes
; n_ptr
; n_ptr
= n_ptr
->next
) {
672 if (!in_scope(domain
, n_ptr
->addr
))
674 for (i
= 0; i
< MAX_BEARERS
; i
++) {
675 if (!n_ptr
->links
[i
])
677 link_info
.dest
= htonl(n_ptr
->addr
);
678 link_info
.up
= htonl(tipc_link_is_up(n_ptr
->links
[i
]));
679 strcpy(link_info
.str
, n_ptr
->links
[i
]->name
);
680 tipc_cfg_append_tlv(buf
, TIPC_TLV_LINK_INFO
,
681 &link_info
, sizeof(link_info
));