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 tipc_node *n_ptr, char *str);
50 static void node_lost_contact(struct tipc_node *n_ptr);
51 static void node_established_contact(struct tipc_node *n_ptr);
53 struct tipc_node *tipc_nodes = NULL; /* sorted list of nodes within cluster */
55 static DEFINE_SPINLOCK(node_create_lock);
60 * tipc_node_create - create neighboring node
62 * Currently, this routine is called by neighbor discovery code, which holds
63 * net_lock for reading only. We must take node_create_lock to ensure a node
64 * isn't created twice if two different bearers discover the node at the same
65 * time. (It would be preferable to switch to holding net_lock in write mode,
66 * but this is a non-trivial change.)
69 struct tipc_node *tipc_node_create(u32 addr)
71 struct cluster *c_ptr;
72 struct tipc_node *n_ptr;
73 struct tipc_node **curr_node;
75 spin_lock_bh(&node_create_lock);
77 for (n_ptr = tipc_nodes; n_ptr; n_ptr = n_ptr->next) {
78 if (addr < n_ptr->addr)
80 if (addr == n_ptr->addr) {
81 spin_unlock_bh(&node_create_lock);
86 n_ptr = kzalloc(sizeof(*n_ptr),GFP_ATOMIC);
88 spin_unlock_bh(&node_create_lock);
89 warn("Node creation failed, no memory\n");
93 c_ptr = tipc_cltr_find(addr);
95 c_ptr = tipc_cltr_create(addr);
98 spin_unlock_bh(&node_create_lock);
104 spin_lock_init(&n_ptr->lock);
105 INIT_LIST_HEAD(&n_ptr->nsub);
106 n_ptr->owner = c_ptr;
107 tipc_cltr_attach_node(c_ptr, n_ptr);
108 n_ptr->last_router = -1;
110 /* Insert node into ordered list */
111 for (curr_node = &tipc_nodes; *curr_node;
112 curr_node = &(*curr_node)->next) {
113 if (addr < (*curr_node)->addr) {
114 n_ptr->next = *curr_node;
118 (*curr_node) = n_ptr;
119 spin_unlock_bh(&node_create_lock);
123 void tipc_node_delete(struct tipc_node *n_ptr)
128 dbg("node %x deleted\n", n_ptr->addr);
134 * tipc_node_link_up - handle addition of link
136 * Link becomes active (alone or shared) or standby, depending on its priority.
139 void tipc_node_link_up(struct tipc_node *n_ptr, struct link *l_ptr)
141 struct link **active = &n_ptr->active_links[0];
143 n_ptr->working_links++;
145 info("Established link <%s> on network plane %c\n",
146 l_ptr->name, l_ptr->b_ptr->net_plane);
149 dbg(" link %x into %x/%x\n", l_ptr, &active[0], &active[1]);
150 active[0] = active[1] = l_ptr;
151 node_established_contact(n_ptr);
154 if (l_ptr->priority < active[0]->priority) {
155 info("New link <%s> becomes standby\n", l_ptr->name);
158 tipc_link_send_duplicate(active[0], l_ptr);
159 if (l_ptr->priority == active[0]->priority) {
163 info("Old link <%s> becomes standby\n", active[0]->name);
164 if (active[1] != active[0])
165 info("Old link <%s> becomes standby\n", active[1]->name);
166 active[0] = active[1] = l_ptr;
170 * node_select_active_links - select active link
173 static void node_select_active_links(struct tipc_node *n_ptr)
175 struct link **active = &n_ptr->active_links[0];
177 u32 highest_prio = 0;
179 active[0] = active[1] = NULL;
181 for (i = 0; i < MAX_BEARERS; i++) {
182 struct link *l_ptr = n_ptr->links[i];
184 if (!l_ptr || !tipc_link_is_up(l_ptr) ||
185 (l_ptr->priority < highest_prio))
188 if (l_ptr->priority > highest_prio) {
189 highest_prio = l_ptr->priority;
190 active[0] = active[1] = l_ptr;
198 * tipc_node_link_down - handle loss of link
201 void tipc_node_link_down(struct tipc_node *n_ptr, struct link *l_ptr)
203 struct link **active;
205 n_ptr->working_links--;
207 if (!tipc_link_is_active(l_ptr)) {
208 info("Lost standby link <%s> on network plane %c\n",
209 l_ptr->name, l_ptr->b_ptr->net_plane);
212 info("Lost link <%s> on network plane %c\n",
213 l_ptr->name, l_ptr->b_ptr->net_plane);
215 active = &n_ptr->active_links[0];
216 if (active[0] == l_ptr)
217 active[0] = active[1];
218 if (active[1] == l_ptr)
219 active[1] = active[0];
220 if (active[0] == l_ptr)
221 node_select_active_links(n_ptr);
222 if (tipc_node_is_up(n_ptr))
223 tipc_link_changeover(l_ptr);
225 node_lost_contact(n_ptr);
228 int tipc_node_has_active_links(struct tipc_node *n_ptr)
230 return n_ptr->active_links[0] != NULL;
233 int tipc_node_has_redundant_links(struct tipc_node *n_ptr)
235 return n_ptr->working_links > 1;
238 static int tipc_node_has_active_routes(struct tipc_node *n_ptr)
240 return n_ptr && (n_ptr->last_router >= 0);
243 int tipc_node_is_up(struct tipc_node *n_ptr)
245 return tipc_node_has_active_links(n_ptr) || tipc_node_has_active_routes(n_ptr);
248 struct tipc_node *tipc_node_attach_link(struct link *l_ptr)
250 struct tipc_node *n_ptr = tipc_node_find(l_ptr->addr);
253 n_ptr = tipc_node_create(l_ptr->addr);
255 u32 bearer_id = l_ptr->b_ptr->identity;
256 char addr_string[16];
258 if (n_ptr->link_cnt >= 2) {
259 err("Attempt to create third link to %s\n",
260 tipc_addr_string_fill(addr_string, n_ptr->addr));
264 if (!n_ptr->links[bearer_id]) {
265 n_ptr->links[bearer_id] = l_ptr;
266 tipc_net.zones[tipc_zone(l_ptr->addr)]->links++;
270 err("Attempt to establish second link on <%s> to %s\n",
271 l_ptr->b_ptr->publ.name,
272 tipc_addr_string_fill(addr_string, l_ptr->addr));
277 void tipc_node_detach_link(struct tipc_node *n_ptr, struct link *l_ptr)
279 n_ptr->links[l_ptr->b_ptr->identity] = NULL;
280 tipc_net.zones[tipc_zone(l_ptr->addr)]->links--;
285 * Routing table management - five cases to handle:
287 * 1: A link towards a zone/cluster external node comes up.
288 * => Send a multicast message updating routing tables of all
289 * system nodes within own cluster that the new destination
290 * can be reached via this node.
291 * (node.establishedContact()=>cluster.multicastNewRoute())
293 * 2: A link towards a slave node comes up.
294 * => Send a multicast message updating routing tables of all
295 * system nodes within own cluster that the new destination
296 * can be reached via this node.
297 * (node.establishedContact()=>cluster.multicastNewRoute())
298 * => Send a message to the slave node about existence
299 * of all system nodes within cluster:
300 * (node.establishedContact()=>cluster.sendLocalRoutes())
302 * 3: A new cluster local system node becomes available.
303 * => Send message(s) to this particular node containing
304 * information about all cluster external and slave
305 * nodes which can be reached via this node.
306 * (node.establishedContact()==>network.sendExternalRoutes())
307 * (node.establishedContact()==>network.sendSlaveRoutes())
308 * => Send messages to all directly connected slave nodes
309 * containing information about the existence of the new node
310 * (node.establishedContact()=>cluster.multicastNewRoute())
312 * 4: The link towards a zone/cluster external node or slave
314 * => Send a multcast message updating routing tables of all
315 * nodes within cluster that the new destination can not any
316 * longer be reached via this node.
317 * (node.lostAllLinks()=>cluster.bcastLostRoute())
319 * 5: A cluster local system node becomes unavailable.
320 * => Remove all references to this node from the local
321 * routing tables. Note: This is a completely node
323 * (node.lostAllLinks()=>network.removeAsRouter())
324 * => Send messages to all directly connected slave nodes
325 * containing information about loss of the node
326 * (node.establishedContact()=>cluster.multicastLostRoute())
330 static void node_established_contact(struct tipc_node *n_ptr)
332 struct cluster *c_ptr;
334 dbg("node_established_contact:-> %x\n", n_ptr->addr);
335 if (!tipc_node_has_active_routes(n_ptr) && in_own_cluster(n_ptr->addr)) {
336 tipc_k_signal((Handler)tipc_named_node_up, n_ptr->addr);
339 /* Syncronize broadcast acks */
340 n_ptr->bclink.acked = tipc_bclink_get_last_sent();
342 if (is_slave(tipc_own_addr))
344 if (!in_own_cluster(n_ptr->addr)) {
345 /* Usage case 1 (see above) */
346 c_ptr = tipc_cltr_find(tipc_own_addr);
348 c_ptr = tipc_cltr_create(tipc_own_addr);
350 tipc_cltr_bcast_new_route(c_ptr, n_ptr->addr, 1,
355 c_ptr = n_ptr->owner;
356 if (is_slave(n_ptr->addr)) {
357 /* Usage case 2 (see above) */
358 tipc_cltr_bcast_new_route(c_ptr, n_ptr->addr, 1, tipc_max_nodes);
359 tipc_cltr_send_local_routes(c_ptr, n_ptr->addr);
363 if (n_ptr->bclink.supported) {
364 tipc_nmap_add(&tipc_cltr_bcast_nodes, n_ptr->addr);
365 if (n_ptr->addr < tipc_own_addr)
369 /* Case 3 (see above) */
370 tipc_net_send_external_routes(n_ptr->addr);
371 tipc_cltr_send_slave_routes(c_ptr, n_ptr->addr);
372 tipc_cltr_bcast_new_route(c_ptr, n_ptr->addr, LOWEST_SLAVE,
373 tipc_highest_allowed_slave);
376 static void node_cleanup_finished(unsigned long node_addr)
378 struct tipc_node *n_ptr;
380 read_lock_bh(&tipc_net_lock);
381 n_ptr = tipc_node_find(node_addr);
383 tipc_node_lock(n_ptr);
384 n_ptr->cleanup_required = 0;
385 tipc_node_unlock(n_ptr);
387 read_unlock_bh(&tipc_net_lock);
390 static void node_lost_contact(struct tipc_node *n_ptr)
392 struct cluster *c_ptr;
393 struct tipc_node_subscr *ns, *tns;
394 char addr_string[16];
397 /* Clean up broadcast reception remains */
398 n_ptr->bclink.gap_after = n_ptr->bclink.gap_to = 0;
399 while (n_ptr->bclink.deferred_head) {
400 struct sk_buff* buf = n_ptr->bclink.deferred_head;
401 n_ptr->bclink.deferred_head = buf->next;
404 if (n_ptr->bclink.defragm) {
405 buf_discard(n_ptr->bclink.defragm);
406 n_ptr->bclink.defragm = NULL;
408 if (in_own_cluster(n_ptr->addr) && n_ptr->bclink.supported) {
409 tipc_bclink_acknowledge(n_ptr, mod(n_ptr->bclink.acked + 10000));
412 /* Update routing tables */
413 if (is_slave(tipc_own_addr)) {
414 tipc_net_remove_as_router(n_ptr->addr);
416 if (!in_own_cluster(n_ptr->addr)) {
417 /* Case 4 (see above) */
418 c_ptr = tipc_cltr_find(tipc_own_addr);
419 tipc_cltr_bcast_lost_route(c_ptr, n_ptr->addr, 1,
422 /* Case 5 (see above) */
423 c_ptr = tipc_cltr_find(n_ptr->addr);
424 if (is_slave(n_ptr->addr)) {
425 tipc_cltr_bcast_lost_route(c_ptr, n_ptr->addr, 1,
428 if (n_ptr->bclink.supported) {
429 tipc_nmap_remove(&tipc_cltr_bcast_nodes,
431 if (n_ptr->addr < tipc_own_addr)
434 tipc_net_remove_as_router(n_ptr->addr);
435 tipc_cltr_bcast_lost_route(c_ptr, n_ptr->addr,
437 tipc_highest_allowed_slave);
441 if (tipc_node_has_active_routes(n_ptr))
444 info("Lost contact with %s\n",
445 tipc_addr_string_fill(addr_string, n_ptr->addr));
447 /* Abort link changeover */
448 for (i = 0; i < MAX_BEARERS; i++) {
449 struct link *l_ptr = n_ptr->links[i];
452 l_ptr->reset_checkpoint = l_ptr->next_in_no;
453 l_ptr->exp_msg_count = 0;
454 tipc_link_reset_fragments(l_ptr);
457 /* Notify subscribers */
458 list_for_each_entry_safe(ns, tns, &n_ptr->nsub, nodesub_list) {
460 list_del_init(&ns->nodesub_list);
461 tipc_k_signal((Handler)ns->handle_node_down,
462 (unsigned long)ns->usr_handle);
465 /* Prevent re-contact with node until all cleanup is done */
467 n_ptr->cleanup_required = 1;
468 tipc_k_signal((Handler)node_cleanup_finished, n_ptr->addr);
472 * tipc_node_select_next_hop - find the next-hop node for a message
474 * Called by when cluster local lookup has failed.
477 struct tipc_node *tipc_node_select_next_hop(u32 addr, u32 selector)
479 struct tipc_node *n_ptr;
482 if (!tipc_addr_domain_valid(addr))
485 /* Look for direct link to destination processsor */
486 n_ptr = tipc_node_find(addr);
487 if (n_ptr && tipc_node_has_active_links(n_ptr))
490 /* Cluster local system nodes *must* have direct links */
491 if (!is_slave(addr) && in_own_cluster(addr))
494 /* Look for cluster local router with direct link to node */
495 router_addr = tipc_node_select_router(n_ptr, selector);
497 return tipc_node_select(router_addr, selector);
499 /* Slave nodes can only be accessed within own cluster via a
500 known router with direct link -- if no router was found,give up */
504 /* Inter zone/cluster -- find any direct link to remote cluster */
505 addr = tipc_addr(tipc_zone(addr), tipc_cluster(addr), 0);
506 n_ptr = tipc_net_select_remote_node(addr, selector);
507 if (n_ptr && tipc_node_has_active_links(n_ptr))
510 /* Last resort -- look for any router to anywhere in remote zone */
511 router_addr = tipc_net_select_router(addr, selector);
513 return tipc_node_select(router_addr, selector);
519 * tipc_node_select_router - select router to reach specified node
521 * Uses a deterministic and fair algorithm for selecting router node.
524 u32 tipc_node_select_router(struct tipc_node *n_ptr, u32 ref)
534 if (n_ptr->last_router < 0)
536 ulim = ((n_ptr->last_router + 1) * 32) - 1;
538 /* Start entry must be random */
539 mask = tipc_max_nodes;
545 /* Lookup upwards with wrap-around */
547 if (((n_ptr->routers[r / 32]) >> (r % 32)) & 1)
549 } while (++r <= ulim);
553 if (((n_ptr->routers[r / 32]) >> (r % 32)) & 1)
555 } while (++r < start);
558 assert(r && (r <= ulim));
559 return tipc_addr(own_zone(), own_cluster(), r);
562 void tipc_node_add_router(struct tipc_node *n_ptr, u32 router)
564 u32 r_num = tipc_node(router);
566 n_ptr->routers[r_num / 32] =
567 ((1 << (r_num % 32)) | n_ptr->routers[r_num / 32]);
568 n_ptr->last_router = tipc_max_nodes / 32;
569 while ((--n_ptr->last_router >= 0) &&
570 !n_ptr->routers[n_ptr->last_router]);
573 void tipc_node_remove_router(struct tipc_node *n_ptr, u32 router)
575 u32 r_num = tipc_node(router);
577 if (n_ptr->last_router < 0)
578 return; /* No routes */
580 n_ptr->routers[r_num / 32] =
581 ((~(1 << (r_num % 32))) & (n_ptr->routers[r_num / 32]));
582 n_ptr->last_router = tipc_max_nodes / 32;
583 while ((--n_ptr->last_router >= 0) &&
584 !n_ptr->routers[n_ptr->last_router]);
586 if (!tipc_node_is_up(n_ptr))
587 node_lost_contact(n_ptr);
590 u32 tipc_available_nodes(const u32 domain)
592 struct tipc_node *n_ptr;
595 read_lock_bh(&tipc_net_lock);
596 for (n_ptr = tipc_nodes; n_ptr; n_ptr = n_ptr->next) {
597 if (!tipc_in_scope(domain, n_ptr->addr))
599 if (tipc_node_is_up(n_ptr))
602 read_unlock_bh(&tipc_net_lock);
606 struct sk_buff *tipc_node_get_nodes(const void *req_tlv_area, int req_tlv_space)
610 struct tipc_node *n_ptr;
611 struct tipc_node_info node_info;
614 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NET_ADDR))
615 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
617 domain = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
618 if (!tipc_addr_domain_valid(domain))
619 return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
620 " (network address)");
622 read_lock_bh(&tipc_net_lock);
624 read_unlock_bh(&tipc_net_lock);
625 return tipc_cfg_reply_none();
628 /* For now, get space for all other nodes
629 (will need to modify this when slave nodes are supported */
631 payload_size = TLV_SPACE(sizeof(node_info)) * (tipc_max_nodes - 1);
632 if (payload_size > 32768u) {
633 read_unlock_bh(&tipc_net_lock);
634 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
635 " (too many nodes)");
637 buf = tipc_cfg_reply_alloc(payload_size);
639 read_unlock_bh(&tipc_net_lock);
643 /* Add TLVs for all nodes in scope */
645 for (n_ptr = tipc_nodes; n_ptr; n_ptr = n_ptr->next) {
646 if (!tipc_in_scope(domain, n_ptr->addr))
648 node_info.addr = htonl(n_ptr->addr);
649 node_info.up = htonl(tipc_node_is_up(n_ptr));
650 tipc_cfg_append_tlv(buf, TIPC_TLV_NODE_INFO,
651 &node_info, sizeof(node_info));
654 read_unlock_bh(&tipc_net_lock);
658 struct sk_buff *tipc_node_get_links(const void *req_tlv_area, int req_tlv_space)
662 struct tipc_node *n_ptr;
663 struct tipc_link_info link_info;
666 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NET_ADDR))
667 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
669 domain = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
670 if (!tipc_addr_domain_valid(domain))
671 return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
672 " (network address)");
674 if (tipc_mode != TIPC_NET_MODE)
675 return tipc_cfg_reply_none();
677 read_lock_bh(&tipc_net_lock);
679 /* Get space for all unicast links + multicast link */
681 payload_size = TLV_SPACE(sizeof(link_info)) *
682 (tipc_net.zones[tipc_zone(tipc_own_addr)]->links + 1);
683 if (payload_size > 32768u) {
684 read_unlock_bh(&tipc_net_lock);
685 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
686 " (too many links)");
688 buf = tipc_cfg_reply_alloc(payload_size);
690 read_unlock_bh(&tipc_net_lock);
694 /* Add TLV for broadcast link */
696 link_info.dest = htonl(tipc_own_addr & 0xfffff00);
697 link_info.up = htonl(1);
698 strlcpy(link_info.str, tipc_bclink_name, TIPC_MAX_LINK_NAME);
699 tipc_cfg_append_tlv(buf, TIPC_TLV_LINK_INFO, &link_info, sizeof(link_info));
701 /* Add TLVs for any other links in scope */
703 for (n_ptr = tipc_nodes; n_ptr; n_ptr = n_ptr->next) {
706 if (!tipc_in_scope(domain, n_ptr->addr))
708 tipc_node_lock(n_ptr);
709 for (i = 0; i < MAX_BEARERS; i++) {
710 if (!n_ptr->links[i])
712 link_info.dest = htonl(n_ptr->addr);
713 link_info.up = htonl(tipc_link_is_up(n_ptr->links[i]));
714 strcpy(link_info.str, n_ptr->links[i]->name);
715 tipc_cfg_append_tlv(buf, TIPC_TLV_LINK_INFO,
716 &link_info, sizeof(link_info));
718 tipc_node_unlock(n_ptr);
721 read_unlock_bh(&tipc_net_lock);