2 * net/tipc/node.c: TIPC node management routines
4 * Copyright (c) 2000-2006, Ericsson AB
5 * Copyright (c) 2005, 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"
50 void node_print(struct print_buf
*buf
, struct node
*n_ptr
, char *str
);
51 static void node_lost_contact(struct node
*n_ptr
);
52 static void node_established_contact(struct node
*n_ptr
);
54 struct node
*nodes
= NULL
; /* sorted list of nodes within cluster */
58 struct node
*node_create(u32 addr
)
60 struct cluster
*c_ptr
;
62 struct node
**curr_node
;
64 n_ptr
= kmalloc(sizeof(*n_ptr
),GFP_ATOMIC
);
66 memset(n_ptr
, 0, sizeof(*n_ptr
));
68 n_ptr
->lock
= SPIN_LOCK_UNLOCKED
;
69 INIT_LIST_HEAD(&n_ptr
->nsub
);
71 c_ptr
= cluster_find(addr
);
73 c_ptr
= cluster_create(addr
);
76 cluster_attach_node(c_ptr
, n_ptr
);
77 n_ptr
->last_router
= -1;
79 /* Insert node into ordered list */
80 for (curr_node
= &nodes
; *curr_node
;
81 curr_node
= &(*curr_node
)->next
) {
82 if (addr
< (*curr_node
)->addr
) {
83 n_ptr
->next
= *curr_node
;
96 void node_delete(struct node
*n_ptr
)
102 /* Not needed because links are already deleted via bearer_stop() */
106 for (l_num
= 0; l_num
< MAX_BEARERS
; l_num
++) {
107 link_delete(n_ptr
->links
[l_num
]);
111 dbg("node %x deleted\n", n_ptr
->addr
);
117 * node_link_up - handle addition of link
119 * Link becomes active (alone or shared) or standby, depending on its priority.
122 void node_link_up(struct node
*n_ptr
, struct link
*l_ptr
)
124 struct link
**active
= &n_ptr
->active_links
[0];
126 info("Established link <%s> on network plane %c\n",
127 l_ptr
->name
, l_ptr
->b_ptr
->net_plane
);
130 dbg(" link %x into %x/%x\n", l_ptr
, &active
[0], &active
[1]);
131 active
[0] = active
[1] = l_ptr
;
132 node_established_contact(n_ptr
);
135 if (l_ptr
->priority
< active
[0]->priority
) {
136 info("Link is standby\n");
139 link_send_duplicate(active
[0], l_ptr
);
140 if (l_ptr
->priority
== active
[0]->priority
) {
144 info("Link <%s> on network plane %c becomes standby\n",
145 active
[0]->name
, active
[0]->b_ptr
->net_plane
);
146 active
[0] = active
[1] = l_ptr
;
150 * node_select_active_links - select active link
153 static void node_select_active_links(struct node
*n_ptr
)
155 struct link
**active
= &n_ptr
->active_links
[0];
157 u32 highest_prio
= 0;
159 active
[0] = active
[1] = 0;
161 for (i
= 0; i
< MAX_BEARERS
; i
++) {
162 struct link
*l_ptr
= n_ptr
->links
[i
];
164 if (!l_ptr
|| !link_is_up(l_ptr
) ||
165 (l_ptr
->priority
< highest_prio
))
168 if (l_ptr
->priority
> highest_prio
) {
169 highest_prio
= l_ptr
->priority
;
170 active
[0] = active
[1] = l_ptr
;
178 * node_link_down - handle loss of link
181 void node_link_down(struct node
*n_ptr
, struct link
*l_ptr
)
183 struct link
**active
;
185 if (!link_is_active(l_ptr
)) {
186 info("Lost standby link <%s> on network plane %c\n",
187 l_ptr
->name
, l_ptr
->b_ptr
->net_plane
);
190 info("Lost link <%s> on network plane %c\n",
191 l_ptr
->name
, l_ptr
->b_ptr
->net_plane
);
193 active
= &n_ptr
->active_links
[0];
194 if (active
[0] == l_ptr
)
195 active
[0] = active
[1];
196 if (active
[1] == l_ptr
)
197 active
[1] = active
[0];
198 if (active
[0] == l_ptr
)
199 node_select_active_links(n_ptr
);
200 if (node_is_up(n_ptr
))
201 link_changeover(l_ptr
);
203 node_lost_contact(n_ptr
);
206 int node_has_active_links(struct node
*n_ptr
)
209 ((n_ptr
->active_links
[0]) || (n_ptr
->active_links
[1])));
212 int node_has_redundant_links(struct node
*n_ptr
)
214 return (node_has_active_links(n_ptr
) &&
215 (n_ptr
->active_links
[0] != n_ptr
->active_links
[1]));
218 int node_has_active_routes(struct node
*n_ptr
)
220 return (n_ptr
&& (n_ptr
->last_router
>= 0));
223 int node_is_up(struct node
*n_ptr
)
225 return (node_has_active_links(n_ptr
) || node_has_active_routes(n_ptr
));
228 struct node
*node_attach_link(struct link
*l_ptr
)
230 struct node
*n_ptr
= node_find(l_ptr
->addr
);
233 n_ptr
= node_create(l_ptr
->addr
);
235 u32 bearer_id
= l_ptr
->b_ptr
->identity
;
236 char addr_string
[16];
238 assert(bearer_id
< MAX_BEARERS
);
239 if (n_ptr
->link_cnt
>= 2) {
240 char addr_string
[16];
242 err("Attempt to create third link to %s\n",
243 addr_string_fill(addr_string
, n_ptr
->addr
));
247 if (!n_ptr
->links
[bearer_id
]) {
248 n_ptr
->links
[bearer_id
] = l_ptr
;
249 net
.zones
[tipc_zone(l_ptr
->addr
)]->links
++;
253 err("Attempt to establish second link on <%s> to <%s> \n",
254 l_ptr
->b_ptr
->publ
.name
,
255 addr_string_fill(addr_string
, l_ptr
->addr
));
260 void node_detach_link(struct node
*n_ptr
, struct link
*l_ptr
)
262 n_ptr
->links
[l_ptr
->b_ptr
->identity
] = 0;
263 net
.zones
[tipc_zone(l_ptr
->addr
)]->links
--;
268 * Routing table management - five cases to handle:
270 * 1: A link towards a zone/cluster external node comes up.
271 * => Send a multicast message updating routing tables of all
272 * system nodes within own cluster that the new destination
273 * can be reached via this node.
274 * (node.establishedContact()=>cluster.multicastNewRoute())
276 * 2: A link towards a slave node comes up.
277 * => Send a multicast message updating routing tables of all
278 * system nodes within own cluster that the new destination
279 * can be reached via this node.
280 * (node.establishedContact()=>cluster.multicastNewRoute())
281 * => Send a message to the slave node about existence
282 * of all system nodes within cluster:
283 * (node.establishedContact()=>cluster.sendLocalRoutes())
285 * 3: A new cluster local system node becomes available.
286 * => Send message(s) to this particular node containing
287 * information about all cluster external and slave
288 * nodes which can be reached via this node.
289 * (node.establishedContact()==>network.sendExternalRoutes())
290 * (node.establishedContact()==>network.sendSlaveRoutes())
291 * => Send messages to all directly connected slave nodes
292 * containing information about the existence of the new node
293 * (node.establishedContact()=>cluster.multicastNewRoute())
295 * 4: The link towards a zone/cluster external node or slave
297 * => Send a multcast message updating routing tables of all
298 * nodes within cluster that the new destination can not any
299 * longer be reached via this node.
300 * (node.lostAllLinks()=>cluster.bcastLostRoute())
302 * 5: A cluster local system node becomes unavailable.
303 * => Remove all references to this node from the local
304 * routing tables. Note: This is a completely node
306 * (node.lostAllLinks()=>network.removeAsRouter())
307 * => Send messages to all directly connected slave nodes
308 * containing information about loss of the node
309 * (node.establishedContact()=>cluster.multicastLostRoute())
313 static void node_established_contact(struct node
*n_ptr
)
315 struct cluster
*c_ptr
;
317 dbg("node_established_contact:-> %x\n", n_ptr
->addr
);
318 if (!node_has_active_routes(n_ptr
)) {
319 k_signal((Handler
)named_node_up
, n_ptr
->addr
);
322 /* Syncronize broadcast acks */
323 n_ptr
->bclink
.acked
= bclink_get_last_sent();
325 if (is_slave(tipc_own_addr
))
327 if (!in_own_cluster(n_ptr
->addr
)) {
328 /* Usage case 1 (see above) */
329 c_ptr
= cluster_find(tipc_own_addr
);
331 c_ptr
= cluster_create(tipc_own_addr
);
333 cluster_bcast_new_route(c_ptr
, n_ptr
->addr
, 1,
338 c_ptr
= n_ptr
->owner
;
339 if (is_slave(n_ptr
->addr
)) {
340 /* Usage case 2 (see above) */
341 cluster_bcast_new_route(c_ptr
, n_ptr
->addr
, 1, tipc_max_nodes
);
342 cluster_send_local_routes(c_ptr
, n_ptr
->addr
);
346 if (n_ptr
->bclink
.supported
) {
347 nmap_add(&cluster_bcast_nodes
, n_ptr
->addr
);
348 if (n_ptr
->addr
< tipc_own_addr
)
352 /* Case 3 (see above) */
353 net_send_external_routes(n_ptr
->addr
);
354 cluster_send_slave_routes(c_ptr
, n_ptr
->addr
);
355 cluster_bcast_new_route(c_ptr
, n_ptr
->addr
, LOWEST_SLAVE
,
356 highest_allowed_slave
);
359 static void node_lost_contact(struct node
*n_ptr
)
361 struct cluster
*c_ptr
;
362 struct node_subscr
*ns
, *tns
;
363 char addr_string
[16];
366 /* Clean up broadcast reception remains */
367 n_ptr
->bclink
.gap_after
= n_ptr
->bclink
.gap_to
= 0;
368 while (n_ptr
->bclink
.deferred_head
) {
369 struct sk_buff
* buf
= n_ptr
->bclink
.deferred_head
;
370 n_ptr
->bclink
.deferred_head
= buf
->next
;
373 if (n_ptr
->bclink
.defragm
) {
374 buf_discard(n_ptr
->bclink
.defragm
);
375 n_ptr
->bclink
.defragm
= NULL
;
377 if (in_own_cluster(n_ptr
->addr
) && n_ptr
->bclink
.supported
) {
378 bclink_acknowledge(n_ptr
, mod(n_ptr
->bclink
.acked
+ 10000));
381 /* Update routing tables */
382 if (is_slave(tipc_own_addr
)) {
383 net_remove_as_router(n_ptr
->addr
);
385 if (!in_own_cluster(n_ptr
->addr
)) {
386 /* Case 4 (see above) */
387 c_ptr
= cluster_find(tipc_own_addr
);
388 cluster_bcast_lost_route(c_ptr
, n_ptr
->addr
, 1,
391 /* Case 5 (see above) */
392 c_ptr
= cluster_find(n_ptr
->addr
);
393 if (is_slave(n_ptr
->addr
)) {
394 cluster_bcast_lost_route(c_ptr
, n_ptr
->addr
, 1,
397 if (n_ptr
->bclink
.supported
) {
398 nmap_remove(&cluster_bcast_nodes
,
400 if (n_ptr
->addr
< tipc_own_addr
)
403 net_remove_as_router(n_ptr
->addr
);
404 cluster_bcast_lost_route(c_ptr
, n_ptr
->addr
,
406 highest_allowed_slave
);
410 if (node_has_active_routes(n_ptr
))
413 info("Lost contact with %s\n",
414 addr_string_fill(addr_string
, n_ptr
->addr
));
416 /* Abort link changeover */
417 for (i
= 0; i
< MAX_BEARERS
; i
++) {
418 struct link
*l_ptr
= n_ptr
->links
[i
];
421 l_ptr
->reset_checkpoint
= l_ptr
->next_in_no
;
422 l_ptr
->exp_msg_count
= 0;
423 link_reset_fragments(l_ptr
);
426 /* Notify subscribers */
427 list_for_each_entry_safe(ns
, tns
, &n_ptr
->nsub
, nodesub_list
) {
429 list_del_init(&ns
->nodesub_list
);
430 k_signal((Handler
)ns
->handle_node_down
,
431 (unsigned long)ns
->usr_handle
);
436 * node_select_next_hop - find the next-hop node for a message
438 * Called by when cluster local lookup has failed.
441 struct node
*node_select_next_hop(u32 addr
, u32 selector
)
446 if (!addr_domain_valid(addr
))
449 /* Look for direct link to destination processsor */
450 n_ptr
= node_find(addr
);
451 if (n_ptr
&& node_has_active_links(n_ptr
))
454 /* Cluster local system nodes *must* have direct links */
455 if (!is_slave(addr
) && in_own_cluster(addr
))
458 /* Look for cluster local router with direct link to node */
459 router_addr
= node_select_router(n_ptr
, selector
);
461 return node_select(router_addr
, selector
);
463 /* Slave nodes can only be accessed within own cluster via a
464 known router with direct link -- if no router was found,give up */
468 /* Inter zone/cluster -- find any direct link to remote cluster */
469 addr
= tipc_addr(tipc_zone(addr
), tipc_cluster(addr
), 0);
470 n_ptr
= net_select_remote_node(addr
, selector
);
471 if (n_ptr
&& node_has_active_links(n_ptr
))
474 /* Last resort -- look for any router to anywhere in remote zone */
475 router_addr
= net_select_router(addr
, selector
);
477 return node_select(router_addr
, selector
);
483 * node_select_router - select router to reach specified node
485 * Uses a deterministic and fair algorithm for selecting router node.
488 u32
node_select_router(struct node
*n_ptr
, u32 ref
)
498 if (n_ptr
->last_router
< 0)
500 ulim
= ((n_ptr
->last_router
+ 1) * 32) - 1;
502 /* Start entry must be random */
503 mask
= tipc_max_nodes
;
509 /* Lookup upwards with wrap-around */
511 if (((n_ptr
->routers
[r
/ 32]) >> (r
% 32)) & 1)
513 } while (++r
<= ulim
);
517 if (((n_ptr
->routers
[r
/ 32]) >> (r
% 32)) & 1)
519 } while (++r
< start
);
522 assert(r
&& (r
<= ulim
));
523 return tipc_addr(own_zone(), own_cluster(), r
);
526 void node_add_router(struct node
*n_ptr
, u32 router
)
528 u32 r_num
= tipc_node(router
);
530 n_ptr
->routers
[r_num
/ 32] =
531 ((1 << (r_num
% 32)) | n_ptr
->routers
[r_num
/ 32]);
532 n_ptr
->last_router
= tipc_max_nodes
/ 32;
533 while ((--n_ptr
->last_router
>= 0) &&
534 !n_ptr
->routers
[n_ptr
->last_router
]);
537 void node_remove_router(struct node
*n_ptr
, u32 router
)
539 u32 r_num
= tipc_node(router
);
541 if (n_ptr
->last_router
< 0)
542 return; /* No routes */
544 n_ptr
->routers
[r_num
/ 32] =
545 ((~(1 << (r_num
% 32))) & (n_ptr
->routers
[r_num
/ 32]));
546 n_ptr
->last_router
= tipc_max_nodes
/ 32;
547 while ((--n_ptr
->last_router
>= 0) &&
548 !n_ptr
->routers
[n_ptr
->last_router
]);
550 if (!node_is_up(n_ptr
))
551 node_lost_contact(n_ptr
);
555 void node_print(struct print_buf
*buf
, struct node
*n_ptr
, char *str
)
559 tipc_printf(buf
, "\n\n%s", str
);
560 for (i
= 0; i
< MAX_BEARERS
; i
++) {
561 if (!n_ptr
->links
[i
])
563 tipc_printf(buf
, "Links[%u]: %x, ", i
, n_ptr
->links
[i
]);
565 tipc_printf(buf
, "Active links: [%x,%x]\n",
566 n_ptr
->active_links
[0], n_ptr
->active_links
[1]);
570 u32
tipc_available_nodes(const u32 domain
)
575 for (n_ptr
= nodes
; n_ptr
; n_ptr
= n_ptr
->next
) {
576 if (!in_scope(domain
, n_ptr
->addr
))
578 if (node_is_up(n_ptr
))
584 struct sk_buff
*node_get_nodes(const void *req_tlv_area
, int req_tlv_space
)
589 struct tipc_node_info node_info
;
591 if (!TLV_CHECK(req_tlv_area
, req_tlv_space
, TIPC_TLV_NET_ADDR
))
592 return cfg_reply_error_string(TIPC_CFG_TLV_ERROR
);
594 domain
= *(u32
*)TLV_DATA(req_tlv_area
);
595 domain
= ntohl(domain
);
596 if (!addr_domain_valid(domain
))
597 return cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
598 " (network address)");
601 return cfg_reply_none();
603 /* For now, get space for all other nodes
604 (will need to modify this when slave nodes are supported */
606 buf
= cfg_reply_alloc(TLV_SPACE(sizeof(node_info
)) *
607 (tipc_max_nodes
- 1));
611 /* Add TLVs for all nodes in scope */
613 for (n_ptr
= nodes
; n_ptr
; n_ptr
= n_ptr
->next
) {
614 if (!in_scope(domain
, n_ptr
->addr
))
616 node_info
.addr
= htonl(n_ptr
->addr
);
617 node_info
.up
= htonl(node_is_up(n_ptr
));
618 cfg_append_tlv(buf
, TIPC_TLV_NODE_INFO
,
619 &node_info
, sizeof(node_info
));
625 struct sk_buff
*node_get_links(const void *req_tlv_area
, int req_tlv_space
)
630 struct tipc_link_info link_info
;
632 if (!TLV_CHECK(req_tlv_area
, req_tlv_space
, TIPC_TLV_NET_ADDR
))
633 return cfg_reply_error_string(TIPC_CFG_TLV_ERROR
);
635 domain
= *(u32
*)TLV_DATA(req_tlv_area
);
636 domain
= ntohl(domain
);
637 if (!addr_domain_valid(domain
))
638 return cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
639 " (network address)");
642 return cfg_reply_none();
644 /* For now, get space for 2 links to all other nodes + bcast link
645 (will need to modify this when slave nodes are supported */
647 buf
= cfg_reply_alloc(TLV_SPACE(sizeof(link_info
)) *
648 (2 * (tipc_max_nodes
- 1) + 1));
652 /* Add TLV for broadcast link */
654 link_info
.dest
= tipc_own_addr
& 0xfffff00;
655 link_info
.dest
= htonl(link_info
.dest
);
656 link_info
.up
= htonl(1);
657 sprintf(link_info
.str
, bc_link_name
);
658 cfg_append_tlv(buf
, TIPC_TLV_LINK_INFO
, &link_info
, sizeof(link_info
));
660 /* Add TLVs for any other links in scope */
662 for (n_ptr
= nodes
; n_ptr
; n_ptr
= n_ptr
->next
) {
665 if (!in_scope(domain
, n_ptr
->addr
))
667 for (i
= 0; i
< MAX_BEARERS
; i
++) {
668 if (!n_ptr
->links
[i
])
670 link_info
.dest
= htonl(n_ptr
->addr
);
671 link_info
.up
= htonl(link_is_up(n_ptr
->links
[i
]));
672 strcpy(link_info
.str
, n_ptr
->links
[i
]->name
);
673 cfg_append_tlv(buf
, TIPC_TLV_LINK_INFO
,
674 &link_info
, sizeof(link_info
));