2 * net/tipc/node.c: TIPC node management routines
4 * Copyright (c) 2003-2005, Ericsson Research Canada
5 * Copyright (c) 2005, Wind River Systems
6 * Copyright (c) 2005-2006, Ericsson AB
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the names of the copyright holders nor the names of its
18 * contributors may be used to endorse or promote products derived from
19 * this software without specific prior written permission.
21 * Alternatively, this software may be distributed under the terms of the
22 * GNU General Public License ("GPL") version 2 as published by the Free
23 * Software Foundation.
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
29 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
44 #include "node_subscr.h"
48 #include "name_distr.h"
51 void node_print(struct print_buf *buf, struct node *n_ptr, char *str);
52 static void node_lost_contact(struct node *n_ptr);
53 static void node_established_contact(struct node *n_ptr);
55 struct node *nodes = NULL; /* sorted list of nodes within cluster */
59 struct node *node_create(u32 addr)
61 struct cluster *c_ptr;
63 struct node **curr_node;
65 n_ptr = kmalloc(sizeof(*n_ptr),GFP_ATOMIC);
67 memset(n_ptr, 0, sizeof(*n_ptr));
69 n_ptr->lock = SPIN_LOCK_UNLOCKED;
70 INIT_LIST_HEAD(&n_ptr->nsub);
72 c_ptr = cluster_find(addr);
74 c_ptr = cluster_create(addr);
77 cluster_attach_node(c_ptr, n_ptr);
78 n_ptr->last_router = -1;
80 /* Insert node into ordered list */
81 for (curr_node = &nodes; *curr_node;
82 curr_node = &(*curr_node)->next) {
83 if (addr < (*curr_node)->addr) {
84 n_ptr->next = *curr_node;
97 void node_delete(struct node *n_ptr)
103 /* Not needed because links are already deleted via 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 * node_link_up - handle addition of link
120 * Link becomes active (alone or shared) or standby, depending on its priority.
123 void node_link_up(struct node *n_ptr, struct link *l_ptr)
125 struct link **active = &n_ptr->active_links[0];
127 info("Established link <%s> on network plane %c\n",
128 l_ptr->name, l_ptr->b_ptr->net_plane);
131 dbg(" link %x into %x/%x\n", l_ptr, &active[0], &active[1]);
132 active[0] = active[1] = l_ptr;
133 node_established_contact(n_ptr);
136 if (l_ptr->priority < active[0]->priority) {
137 info("Link is standby\n");
140 link_send_duplicate(active[0], l_ptr);
141 if (l_ptr->priority == active[0]->priority) {
145 info("Link <%s> on network plane %c becomes standby\n",
146 active[0]->name, active[0]->b_ptr->net_plane);
147 active[0] = active[1] = l_ptr;
151 * node_select_active_links - select active link
154 static void node_select_active_links(struct node *n_ptr)
156 struct link **active = &n_ptr->active_links[0];
158 u32 highest_prio = 0;
160 active[0] = active[1] = 0;
162 for (i = 0; i < MAX_BEARERS; i++) {
163 struct link *l_ptr = n_ptr->links[i];
165 if (!l_ptr || !link_is_up(l_ptr) ||
166 (l_ptr->priority < highest_prio))
169 if (l_ptr->priority > highest_prio) {
170 highest_prio = l_ptr->priority;
171 active[0] = active[1] = l_ptr;
179 * node_link_down - handle loss of link
182 void node_link_down(struct node *n_ptr, struct link *l_ptr)
184 struct link **active;
186 if (!link_is_active(l_ptr)) {
187 info("Lost standby link <%s> on network plane %c\n",
188 l_ptr->name, l_ptr->b_ptr->net_plane);
191 info("Lost link <%s> on network plane %c\n",
192 l_ptr->name, l_ptr->b_ptr->net_plane);
194 active = &n_ptr->active_links[0];
195 if (active[0] == l_ptr)
196 active[0] = active[1];
197 if (active[1] == l_ptr)
198 active[1] = active[0];
199 if (active[0] == l_ptr)
200 node_select_active_links(n_ptr);
201 if (node_is_up(n_ptr))
202 link_changeover(l_ptr);
204 node_lost_contact(n_ptr);
207 int node_has_active_links(struct node *n_ptr)
210 ((n_ptr->active_links[0]) || (n_ptr->active_links[1])));
213 int node_has_redundant_links(struct node *n_ptr)
215 return (node_has_active_links(n_ptr) &&
216 (n_ptr->active_links[0] != n_ptr->active_links[1]));
219 int node_has_active_routes(struct node *n_ptr)
221 return (n_ptr && (n_ptr->last_router >= 0));
224 int node_is_up(struct node *n_ptr)
226 return (node_has_active_links(n_ptr) || node_has_active_routes(n_ptr));
229 struct node *node_attach_link(struct link *l_ptr)
231 struct node *n_ptr = node_find(l_ptr->addr);
234 n_ptr = node_create(l_ptr->addr);
236 u32 bearer_id = l_ptr->b_ptr->identity;
237 char addr_string[16];
239 assert(bearer_id < MAX_BEARERS);
240 if (n_ptr->link_cnt >= 2) {
241 char addr_string[16];
243 err("Attempt to create third link to %s\n",
244 addr_string_fill(addr_string, n_ptr->addr));
248 if (!n_ptr->links[bearer_id]) {
249 n_ptr->links[bearer_id] = l_ptr;
250 net.zones[tipc_zone(l_ptr->addr)]->links++;
254 err("Attempt to establish second link on <%s> to <%s> \n",
255 l_ptr->b_ptr->publ.name,
256 addr_string_fill(addr_string, l_ptr->addr));
261 void node_detach_link(struct node *n_ptr, struct link *l_ptr)
263 n_ptr->links[l_ptr->b_ptr->identity] = 0;
264 net.zones[tipc_zone(l_ptr->addr)]->links--;
269 * Routing table management - five cases to handle:
271 * 1: A link towards a zone/cluster external node comes up.
272 * => Send a multicast message updating routing tables of all
273 * system nodes within own cluster that the new destination
274 * can be reached via this node.
275 * (node.establishedContact()=>cluster.multicastNewRoute())
277 * 2: A link towards a slave node comes up.
278 * => Send a multicast message updating routing tables of all
279 * system nodes within own cluster that the new destination
280 * can be reached via this node.
281 * (node.establishedContact()=>cluster.multicastNewRoute())
282 * => Send a message to the slave node about existence
283 * of all system nodes within cluster:
284 * (node.establishedContact()=>cluster.sendLocalRoutes())
286 * 3: A new cluster local system node becomes available.
287 * => Send message(s) to this particular node containing
288 * information about all cluster external and slave
289 * nodes which can be reached via this node.
290 * (node.establishedContact()==>network.sendExternalRoutes())
291 * (node.establishedContact()==>network.sendSlaveRoutes())
292 * => Send messages to all directly connected slave nodes
293 * containing information about the existence of the new node
294 * (node.establishedContact()=>cluster.multicastNewRoute())
296 * 4: The link towards a zone/cluster external node or slave
298 * => Send a multcast message updating routing tables of all
299 * nodes within cluster that the new destination can not any
300 * longer be reached via this node.
301 * (node.lostAllLinks()=>cluster.bcastLostRoute())
303 * 5: A cluster local system node becomes unavailable.
304 * => Remove all references to this node from the local
305 * routing tables. Note: This is a completely node
307 * (node.lostAllLinks()=>network.removeAsRouter())
308 * => Send messages to all directly connected slave nodes
309 * containing information about loss of the node
310 * (node.establishedContact()=>cluster.multicastLostRoute())
314 static void node_established_contact(struct node *n_ptr)
316 struct cluster *c_ptr;
318 dbg("node_established_contact:-> %x\n", n_ptr->addr);
319 if (!node_has_active_routes(n_ptr)) {
320 k_signal((Handler)named_node_up, n_ptr->addr);
323 /* Syncronize broadcast acks */
324 n_ptr->bclink.acked = bclink_get_last_sent();
326 if (is_slave(tipc_own_addr))
328 if (!in_own_cluster(n_ptr->addr)) {
329 /* Usage case 1 (see above) */
330 c_ptr = cluster_find(tipc_own_addr);
332 c_ptr = cluster_create(tipc_own_addr);
334 cluster_bcast_new_route(c_ptr, n_ptr->addr, 1,
339 c_ptr = n_ptr->owner;
340 if (is_slave(n_ptr->addr)) {
341 /* Usage case 2 (see above) */
342 cluster_bcast_new_route(c_ptr, n_ptr->addr, 1, tipc_max_nodes);
343 cluster_send_local_routes(c_ptr, n_ptr->addr);
347 if (n_ptr->bclink.supported) {
348 nmap_add(&cluster_bcast_nodes, n_ptr->addr);
349 if (n_ptr->addr < tipc_own_addr)
353 /* Case 3 (see above) */
354 net_send_external_routes(n_ptr->addr);
355 cluster_send_slave_routes(c_ptr, n_ptr->addr);
356 cluster_bcast_new_route(c_ptr, n_ptr->addr, LOWEST_SLAVE,
357 highest_allowed_slave);
360 static void node_lost_contact(struct node *n_ptr)
362 struct cluster *c_ptr;
363 struct node_subscr *ns, *tns;
364 char addr_string[16];
367 /* Clean up broadcast reception remains */
368 n_ptr->bclink.gap_after = n_ptr->bclink.gap_to = 0;
369 while (n_ptr->bclink.deferred_head) {
370 struct sk_buff* buf = n_ptr->bclink.deferred_head;
371 n_ptr->bclink.deferred_head = buf->next;
374 if (n_ptr->bclink.defragm) {
375 buf_discard(n_ptr->bclink.defragm);
376 n_ptr->bclink.defragm = NULL;
378 if (in_own_cluster(n_ptr->addr) && n_ptr->bclink.supported) {
379 bclink_acknowledge(n_ptr, mod(n_ptr->bclink.acked + 10000));
382 /* Update routing tables */
383 if (is_slave(tipc_own_addr)) {
384 net_remove_as_router(n_ptr->addr);
386 if (!in_own_cluster(n_ptr->addr)) {
387 /* Case 4 (see above) */
388 c_ptr = cluster_find(tipc_own_addr);
389 cluster_bcast_lost_route(c_ptr, n_ptr->addr, 1,
392 /* Case 5 (see above) */
393 c_ptr = cluster_find(n_ptr->addr);
394 if (is_slave(n_ptr->addr)) {
395 cluster_bcast_lost_route(c_ptr, n_ptr->addr, 1,
398 if (n_ptr->bclink.supported) {
399 nmap_remove(&cluster_bcast_nodes,
401 if (n_ptr->addr < tipc_own_addr)
404 net_remove_as_router(n_ptr->addr);
405 cluster_bcast_lost_route(c_ptr, n_ptr->addr,
407 highest_allowed_slave);
411 if (node_has_active_routes(n_ptr))
414 info("Lost contact with %s\n",
415 addr_string_fill(addr_string, n_ptr->addr));
417 /* Abort link changeover */
418 for (i = 0; i < MAX_BEARERS; i++) {
419 struct link *l_ptr = n_ptr->links[i];
422 l_ptr->reset_checkpoint = l_ptr->next_in_no;
423 l_ptr->exp_msg_count = 0;
424 link_reset_fragments(l_ptr);
427 /* Notify subscribers */
428 list_for_each_entry_safe(ns, tns, &n_ptr->nsub, nodesub_list) {
430 list_del_init(&ns->nodesub_list);
431 k_signal((Handler)ns->handle_node_down,
432 (unsigned long)ns->usr_handle);
437 * node_select_next_hop - find the next-hop node for a message
439 * Called by when cluster local lookup has failed.
442 struct node *node_select_next_hop(u32 addr, u32 selector)
447 if (!addr_domain_valid(addr))
450 /* Look for direct link to destination processsor */
451 n_ptr = node_find(addr);
452 if (n_ptr && node_has_active_links(n_ptr))
455 /* Cluster local system nodes *must* have direct links */
456 if (!is_slave(addr) && in_own_cluster(addr))
459 /* Look for cluster local router with direct link to node */
460 router_addr = node_select_router(n_ptr, selector);
462 return node_select(router_addr, selector);
464 /* Slave nodes can only be accessed within own cluster via a
465 known router with direct link -- if no router was found,give up */
469 /* Inter zone/cluster -- find any direct link to remote cluster */
470 addr = tipc_addr(tipc_zone(addr), tipc_cluster(addr), 0);
471 n_ptr = net_select_remote_node(addr, selector);
472 if (n_ptr && node_has_active_links(n_ptr))
475 /* Last resort -- look for any router to anywhere in remote zone */
476 router_addr = net_select_router(addr, selector);
478 return node_select(router_addr, selector);
484 * node_select_router - select router to reach specified node
486 * Uses a deterministic and fair algorithm for selecting router node.
489 u32 node_select_router(struct node *n_ptr, u32 ref)
499 if (n_ptr->last_router < 0)
501 ulim = ((n_ptr->last_router + 1) * 32) - 1;
503 /* Start entry must be random */
504 mask = tipc_max_nodes;
510 /* Lookup upwards with wrap-around */
512 if (((n_ptr->routers[r / 32]) >> (r % 32)) & 1)
514 } while (++r <= ulim);
518 if (((n_ptr->routers[r / 32]) >> (r % 32)) & 1)
520 } while (++r < start);
523 assert(r && (r <= ulim));
524 return tipc_addr(own_zone(), own_cluster(), r);
527 void node_add_router(struct node *n_ptr, u32 router)
529 u32 r_num = tipc_node(router);
531 n_ptr->routers[r_num / 32] =
532 ((1 << (r_num % 32)) | n_ptr->routers[r_num / 32]);
533 n_ptr->last_router = tipc_max_nodes / 32;
534 while ((--n_ptr->last_router >= 0) &&
535 !n_ptr->routers[n_ptr->last_router]);
538 void node_remove_router(struct node *n_ptr, u32 router)
540 u32 r_num = tipc_node(router);
542 if (n_ptr->last_router < 0)
543 return; /* No routes */
545 n_ptr->routers[r_num / 32] =
546 ((~(1 << (r_num % 32))) & (n_ptr->routers[r_num / 32]));
547 n_ptr->last_router = tipc_max_nodes / 32;
548 while ((--n_ptr->last_router >= 0) &&
549 !n_ptr->routers[n_ptr->last_router]);
551 if (!node_is_up(n_ptr))
552 node_lost_contact(n_ptr);
556 void node_print(struct print_buf *buf, struct node *n_ptr, char *str)
560 tipc_printf(buf, "\n\n%s", str);
561 for (i = 0; i < MAX_BEARERS; i++) {
562 if (!n_ptr->links[i])
564 tipc_printf(buf, "Links[%u]: %x, ", i, n_ptr->links[i]);
566 tipc_printf(buf, "Active links: [%x,%x]\n",
567 n_ptr->active_links[0], n_ptr->active_links[1]);
571 u32 tipc_available_nodes(const u32 domain)
576 for (n_ptr = nodes; n_ptr; n_ptr = n_ptr->next) {
577 if (!in_scope(domain, n_ptr->addr))
579 if (node_is_up(n_ptr))
585 struct sk_buff *node_get_nodes(const void *req_tlv_area, int req_tlv_space)
590 struct tipc_node_info node_info;
592 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NET_ADDR))
593 return cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
595 domain = *(u32 *)TLV_DATA(req_tlv_area);
596 domain = ntohl(domain);
597 if (!addr_domain_valid(domain))
598 return cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
599 " (network address)");
602 return cfg_reply_none();
604 /* For now, get space for all other nodes
605 (will need to modify this when slave nodes are supported */
607 buf = cfg_reply_alloc(TLV_SPACE(sizeof(node_info)) *
608 (tipc_max_nodes - 1));
612 /* Add TLVs for all nodes in scope */
614 for (n_ptr = nodes; n_ptr; n_ptr = n_ptr->next) {
615 if (!in_scope(domain, n_ptr->addr))
617 node_info.addr = htonl(n_ptr->addr);
618 node_info.up = htonl(node_is_up(n_ptr));
619 cfg_append_tlv(buf, TIPC_TLV_NODE_INFO,
620 &node_info, sizeof(node_info));
626 struct sk_buff *node_get_links(const void *req_tlv_area, int req_tlv_space)
631 struct tipc_link_info link_info;
633 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NET_ADDR))
634 return cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
636 domain = *(u32 *)TLV_DATA(req_tlv_area);
637 domain = ntohl(domain);
638 if (!addr_domain_valid(domain))
639 return cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
640 " (network address)");
643 return cfg_reply_none();
645 /* For now, get space for 2 links to all other nodes + bcast link
646 (will need to modify this when slave nodes are supported */
648 buf = cfg_reply_alloc(TLV_SPACE(sizeof(link_info)) *
649 (2 * (tipc_max_nodes - 1) + 1));
653 /* Add TLV for broadcast link */
655 link_info.dest = tipc_own_addr & 0xfffff00;
656 link_info.dest = htonl(link_info.dest);
657 link_info.up = htonl(1);
658 sprintf(link_info.str, bc_link_name);
659 cfg_append_tlv(buf, TIPC_TLV_LINK_INFO, &link_info, sizeof(link_info));
661 /* Add TLVs for any other links in scope */
663 for (n_ptr = nodes; n_ptr; n_ptr = n_ptr->next) {
666 if (!in_scope(domain, n_ptr->addr))
668 for (i = 0; i < MAX_BEARERS; i++) {
669 if (!n_ptr->links[i])
671 link_info.dest = htonl(n_ptr->addr);
672 link_info.up = htonl(link_is_up(n_ptr->links[i]));
673 strcpy(link_info.str, n_ptr->links[i]->name);
674 cfg_append_tlv(buf, TIPC_TLV_LINK_INFO,
675 &link_info, sizeof(link_info));