[PATCH] f71805f: Resource needs not be global
[linux-2.6/x86.git] / net / tipc / node.c
blob0d5db06e203f59a4da46b8a8182e86c7f5e462a7
1 /*
2 * net/tipc/node.c: TIPC node management routines
3 *
4 * Copyright (c) 2000-2006, Ericsson AB
5 * Copyright (c) 2005, Wind River Systems
6 * All rights reserved.
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.
37 #include "core.h"
38 #include "config.h"
39 #include "node.h"
40 #include "cluster.h"
41 #include "net.h"
42 #include "addr.h"
43 #include "node_subscr.h"
44 #include "link.h"
45 #include "port.h"
46 #include "bearer.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 */
55 u32 tipc_own_tag = 0;
57 struct node *tipc_node_create(u32 addr)
59 struct cluster *c_ptr;
60 struct node *n_ptr;
61 struct node **curr_node;
63 n_ptr = kmalloc(sizeof(*n_ptr),GFP_ATOMIC);
64 if (n_ptr != NULL) {
65 memset(n_ptr, 0, sizeof(*n_ptr));
66 n_ptr->addr = addr;
67 n_ptr->lock = SPIN_LOCK_UNLOCKED;
68 INIT_LIST_HEAD(&n_ptr->nsub);
70 c_ptr = tipc_cltr_find(addr);
71 if (c_ptr == NULL)
72 c_ptr = tipc_cltr_create(addr);
73 if (c_ptr != NULL) {
74 n_ptr->owner = c_ptr;
75 tipc_cltr_attach_node(c_ptr, n_ptr);
76 n_ptr->last_router = -1;
78 /* Insert node into ordered list */
79 for (curr_node = &tipc_nodes; *curr_node;
80 curr_node = &(*curr_node)->next) {
81 if (addr < (*curr_node)->addr) {
82 n_ptr->next = *curr_node;
83 break;
86 (*curr_node) = n_ptr;
87 } else {
88 kfree(n_ptr);
89 n_ptr = NULL;
92 return n_ptr;
95 void tipc_node_delete(struct node *n_ptr)
97 if (!n_ptr)
98 return;
100 #if 0
101 /* Not needed because links are already deleted via tipc_bearer_stop() */
103 u32 l_num;
105 for (l_num = 0; l_num < MAX_BEARERS; l_num++) {
106 link_delete(n_ptr->links[l_num]);
108 #endif
110 dbg("node %x deleted\n", n_ptr->addr);
111 kfree(n_ptr);
116 * tipc_node_link_up - handle addition of link
118 * Link becomes active (alone or shared) or standby, depending on its priority.
121 void tipc_node_link_up(struct node *n_ptr, struct link *l_ptr)
123 struct link **active = &n_ptr->active_links[0];
125 info("Established link <%s> on network plane %c\n",
126 l_ptr->name, l_ptr->b_ptr->net_plane);
128 if (!active[0]) {
129 dbg(" link %x into %x/%x\n", l_ptr, &active[0], &active[1]);
130 active[0] = active[1] = l_ptr;
131 node_established_contact(n_ptr);
132 return;
134 if (l_ptr->priority < active[0]->priority) {
135 info("Link is standby\n");
136 return;
138 tipc_link_send_duplicate(active[0], l_ptr);
139 if (l_ptr->priority == active[0]->priority) {
140 active[0] = l_ptr;
141 return;
143 info("Link <%s> on network plane %c becomes standby\n",
144 active[0]->name, active[0]->b_ptr->net_plane);
145 active[0] = active[1] = l_ptr;
149 * node_select_active_links - select active link
152 static void node_select_active_links(struct node *n_ptr)
154 struct link **active = &n_ptr->active_links[0];
155 u32 i;
156 u32 highest_prio = 0;
158 active[0] = active[1] = NULL;
160 for (i = 0; i < MAX_BEARERS; i++) {
161 struct link *l_ptr = n_ptr->links[i];
163 if (!l_ptr || !tipc_link_is_up(l_ptr) ||
164 (l_ptr->priority < highest_prio))
165 continue;
167 if (l_ptr->priority > highest_prio) {
168 highest_prio = l_ptr->priority;
169 active[0] = active[1] = l_ptr;
170 } else {
171 active[1] = l_ptr;
177 * tipc_node_link_down - handle loss of link
180 void tipc_node_link_down(struct node *n_ptr, struct link *l_ptr)
182 struct link **active;
184 if (!tipc_link_is_active(l_ptr)) {
185 info("Lost standby link <%s> on network plane %c\n",
186 l_ptr->name, l_ptr->b_ptr->net_plane);
187 return;
189 info("Lost link <%s> on network plane %c\n",
190 l_ptr->name, l_ptr->b_ptr->net_plane);
192 active = &n_ptr->active_links[0];
193 if (active[0] == l_ptr)
194 active[0] = active[1];
195 if (active[1] == l_ptr)
196 active[1] = active[0];
197 if (active[0] == l_ptr)
198 node_select_active_links(n_ptr);
199 if (tipc_node_is_up(n_ptr))
200 tipc_link_changeover(l_ptr);
201 else
202 node_lost_contact(n_ptr);
205 int tipc_node_has_active_links(struct node *n_ptr)
207 return (n_ptr &&
208 ((n_ptr->active_links[0]) || (n_ptr->active_links[1])));
211 int tipc_node_has_redundant_links(struct node *n_ptr)
213 return (tipc_node_has_active_links(n_ptr) &&
214 (n_ptr->active_links[0] != n_ptr->active_links[1]));
217 static int tipc_node_has_active_routes(struct node *n_ptr)
219 return (n_ptr && (n_ptr->last_router >= 0));
222 int tipc_node_is_up(struct node *n_ptr)
224 return (tipc_node_has_active_links(n_ptr) || tipc_node_has_active_routes(n_ptr));
227 struct node *tipc_node_attach_link(struct link *l_ptr)
229 struct node *n_ptr = tipc_node_find(l_ptr->addr);
231 if (!n_ptr)
232 n_ptr = tipc_node_create(l_ptr->addr);
233 if (n_ptr) {
234 u32 bearer_id = l_ptr->b_ptr->identity;
235 char addr_string[16];
237 assert(bearer_id < MAX_BEARERS);
238 if (n_ptr->link_cnt >= 2) {
239 char addr_string[16];
241 err("Attempt to create third link to %s\n",
242 addr_string_fill(addr_string, n_ptr->addr));
243 return NULL;
246 if (!n_ptr->links[bearer_id]) {
247 n_ptr->links[bearer_id] = l_ptr;
248 tipc_net.zones[tipc_zone(l_ptr->addr)]->links++;
249 n_ptr->link_cnt++;
250 return n_ptr;
252 err("Attempt to establish second link on <%s> to <%s> \n",
253 l_ptr->b_ptr->publ.name,
254 addr_string_fill(addr_string, l_ptr->addr));
256 return NULL;
259 void tipc_node_detach_link(struct node *n_ptr, struct link *l_ptr)
261 n_ptr->links[l_ptr->b_ptr->identity] = NULL;
262 tipc_net.zones[tipc_zone(l_ptr->addr)]->links--;
263 n_ptr->link_cnt--;
267 * Routing table management - five cases to handle:
269 * 1: A link towards a zone/cluster external node comes up.
270 * => Send a multicast message updating routing tables of all
271 * system nodes within own cluster that the new destination
272 * can be reached via this node.
273 * (node.establishedContact()=>cluster.multicastNewRoute())
275 * 2: A link towards a slave node comes up.
276 * => Send a multicast message updating routing tables of all
277 * system nodes within own cluster that the new destination
278 * can be reached via this node.
279 * (node.establishedContact()=>cluster.multicastNewRoute())
280 * => Send a message to the slave node about existence
281 * of all system nodes within cluster:
282 * (node.establishedContact()=>cluster.sendLocalRoutes())
284 * 3: A new cluster local system node becomes available.
285 * => Send message(s) to this particular node containing
286 * information about all cluster external and slave
287 * nodes which can be reached via this node.
288 * (node.establishedContact()==>network.sendExternalRoutes())
289 * (node.establishedContact()==>network.sendSlaveRoutes())
290 * => Send messages to all directly connected slave nodes
291 * containing information about the existence of the new node
292 * (node.establishedContact()=>cluster.multicastNewRoute())
294 * 4: The link towards a zone/cluster external node or slave
295 * node goes down.
296 * => Send a multcast message updating routing tables of all
297 * nodes within cluster that the new destination can not any
298 * longer be reached via this node.
299 * (node.lostAllLinks()=>cluster.bcastLostRoute())
301 * 5: A cluster local system node becomes unavailable.
302 * => Remove all references to this node from the local
303 * routing tables. Note: This is a completely node
304 * local operation.
305 * (node.lostAllLinks()=>network.removeAsRouter())
306 * => Send messages to all directly connected slave nodes
307 * containing information about loss of the node
308 * (node.establishedContact()=>cluster.multicastLostRoute())
312 static void node_established_contact(struct node *n_ptr)
314 struct cluster *c_ptr;
316 dbg("node_established_contact:-> %x\n", n_ptr->addr);
317 if (!tipc_node_has_active_routes(n_ptr)) {
318 tipc_k_signal((Handler)tipc_named_node_up, n_ptr->addr);
321 /* Syncronize broadcast acks */
322 n_ptr->bclink.acked = tipc_bclink_get_last_sent();
324 if (is_slave(tipc_own_addr))
325 return;
326 if (!in_own_cluster(n_ptr->addr)) {
327 /* Usage case 1 (see above) */
328 c_ptr = tipc_cltr_find(tipc_own_addr);
329 if (!c_ptr)
330 c_ptr = tipc_cltr_create(tipc_own_addr);
331 if (c_ptr)
332 tipc_cltr_bcast_new_route(c_ptr, n_ptr->addr, 1,
333 tipc_max_nodes);
334 return;
337 c_ptr = n_ptr->owner;
338 if (is_slave(n_ptr->addr)) {
339 /* Usage case 2 (see above) */
340 tipc_cltr_bcast_new_route(c_ptr, n_ptr->addr, 1, tipc_max_nodes);
341 tipc_cltr_send_local_routes(c_ptr, n_ptr->addr);
342 return;
345 if (n_ptr->bclink.supported) {
346 tipc_nmap_add(&tipc_cltr_bcast_nodes, n_ptr->addr);
347 if (n_ptr->addr < tipc_own_addr)
348 tipc_own_tag++;
351 /* Case 3 (see above) */
352 tipc_net_send_external_routes(n_ptr->addr);
353 tipc_cltr_send_slave_routes(c_ptr, n_ptr->addr);
354 tipc_cltr_bcast_new_route(c_ptr, n_ptr->addr, LOWEST_SLAVE,
355 tipc_highest_allowed_slave);
358 static void node_lost_contact(struct node *n_ptr)
360 struct cluster *c_ptr;
361 struct node_subscr *ns, *tns;
362 char addr_string[16];
363 u32 i;
365 /* Clean up broadcast reception remains */
366 n_ptr->bclink.gap_after = n_ptr->bclink.gap_to = 0;
367 while (n_ptr->bclink.deferred_head) {
368 struct sk_buff* buf = n_ptr->bclink.deferred_head;
369 n_ptr->bclink.deferred_head = buf->next;
370 buf_discard(buf);
372 if (n_ptr->bclink.defragm) {
373 buf_discard(n_ptr->bclink.defragm);
374 n_ptr->bclink.defragm = NULL;
376 if (in_own_cluster(n_ptr->addr) && n_ptr->bclink.supported) {
377 tipc_bclink_acknowledge(n_ptr, mod(n_ptr->bclink.acked + 10000));
380 /* Update routing tables */
381 if (is_slave(tipc_own_addr)) {
382 tipc_net_remove_as_router(n_ptr->addr);
383 } else {
384 if (!in_own_cluster(n_ptr->addr)) {
385 /* Case 4 (see above) */
386 c_ptr = tipc_cltr_find(tipc_own_addr);
387 tipc_cltr_bcast_lost_route(c_ptr, n_ptr->addr, 1,
388 tipc_max_nodes);
389 } else {
390 /* Case 5 (see above) */
391 c_ptr = tipc_cltr_find(n_ptr->addr);
392 if (is_slave(n_ptr->addr)) {
393 tipc_cltr_bcast_lost_route(c_ptr, n_ptr->addr, 1,
394 tipc_max_nodes);
395 } else {
396 if (n_ptr->bclink.supported) {
397 tipc_nmap_remove(&tipc_cltr_bcast_nodes,
398 n_ptr->addr);
399 if (n_ptr->addr < tipc_own_addr)
400 tipc_own_tag--;
402 tipc_net_remove_as_router(n_ptr->addr);
403 tipc_cltr_bcast_lost_route(c_ptr, n_ptr->addr,
404 LOWEST_SLAVE,
405 tipc_highest_allowed_slave);
409 if (tipc_node_has_active_routes(n_ptr))
410 return;
412 info("Lost contact with %s\n",
413 addr_string_fill(addr_string, n_ptr->addr));
415 /* Abort link changeover */
416 for (i = 0; i < MAX_BEARERS; i++) {
417 struct link *l_ptr = n_ptr->links[i];
418 if (!l_ptr)
419 continue;
420 l_ptr->reset_checkpoint = l_ptr->next_in_no;
421 l_ptr->exp_msg_count = 0;
422 tipc_link_reset_fragments(l_ptr);
425 /* Notify subscribers */
426 list_for_each_entry_safe(ns, tns, &n_ptr->nsub, nodesub_list) {
427 ns->node = NULL;
428 list_del_init(&ns->nodesub_list);
429 tipc_k_signal((Handler)ns->handle_node_down,
430 (unsigned long)ns->usr_handle);
435 * tipc_node_select_next_hop - find the next-hop node for a message
437 * Called by when cluster local lookup has failed.
440 struct node *tipc_node_select_next_hop(u32 addr, u32 selector)
442 struct node *n_ptr;
443 u32 router_addr;
445 if (!tipc_addr_domain_valid(addr))
446 return NULL;
448 /* Look for direct link to destination processsor */
449 n_ptr = tipc_node_find(addr);
450 if (n_ptr && tipc_node_has_active_links(n_ptr))
451 return n_ptr;
453 /* Cluster local system nodes *must* have direct links */
454 if (!is_slave(addr) && in_own_cluster(addr))
455 return NULL;
457 /* Look for cluster local router with direct link to node */
458 router_addr = tipc_node_select_router(n_ptr, selector);
459 if (router_addr)
460 return tipc_node_select(router_addr, selector);
462 /* Slave nodes can only be accessed within own cluster via a
463 known router with direct link -- if no router was found,give up */
464 if (is_slave(addr))
465 return NULL;
467 /* Inter zone/cluster -- find any direct link to remote cluster */
468 addr = tipc_addr(tipc_zone(addr), tipc_cluster(addr), 0);
469 n_ptr = tipc_net_select_remote_node(addr, selector);
470 if (n_ptr && tipc_node_has_active_links(n_ptr))
471 return n_ptr;
473 /* Last resort -- look for any router to anywhere in remote zone */
474 router_addr = tipc_net_select_router(addr, selector);
475 if (router_addr)
476 return tipc_node_select(router_addr, selector);
478 return NULL;
482 * tipc_node_select_router - select router to reach specified node
484 * Uses a deterministic and fair algorithm for selecting router node.
487 u32 tipc_node_select_router(struct node *n_ptr, u32 ref)
489 u32 ulim;
490 u32 mask;
491 u32 start;
492 u32 r;
494 if (!n_ptr)
495 return 0;
497 if (n_ptr->last_router < 0)
498 return 0;
499 ulim = ((n_ptr->last_router + 1) * 32) - 1;
501 /* Start entry must be random */
502 mask = tipc_max_nodes;
503 while (mask > ulim)
504 mask >>= 1;
505 start = ref & mask;
506 r = start;
508 /* Lookup upwards with wrap-around */
509 do {
510 if (((n_ptr->routers[r / 32]) >> (r % 32)) & 1)
511 break;
512 } while (++r <= ulim);
513 if (r > ulim) {
514 r = 1;
515 do {
516 if (((n_ptr->routers[r / 32]) >> (r % 32)) & 1)
517 break;
518 } while (++r < start);
519 assert(r != start);
521 assert(r && (r <= ulim));
522 return tipc_addr(own_zone(), own_cluster(), r);
525 void tipc_node_add_router(struct node *n_ptr, u32 router)
527 u32 r_num = tipc_node(router);
529 n_ptr->routers[r_num / 32] =
530 ((1 << (r_num % 32)) | n_ptr->routers[r_num / 32]);
531 n_ptr->last_router = tipc_max_nodes / 32;
532 while ((--n_ptr->last_router >= 0) &&
533 !n_ptr->routers[n_ptr->last_router]);
536 void tipc_node_remove_router(struct node *n_ptr, u32 router)
538 u32 r_num = tipc_node(router);
540 if (n_ptr->last_router < 0)
541 return; /* No routes */
543 n_ptr->routers[r_num / 32] =
544 ((~(1 << (r_num % 32))) & (n_ptr->routers[r_num / 32]));
545 n_ptr->last_router = tipc_max_nodes / 32;
546 while ((--n_ptr->last_router >= 0) &&
547 !n_ptr->routers[n_ptr->last_router]);
549 if (!tipc_node_is_up(n_ptr))
550 node_lost_contact(n_ptr);
553 #if 0
554 void node_print(struct print_buf *buf, struct node *n_ptr, char *str)
556 u32 i;
558 tipc_printf(buf, "\n\n%s", str);
559 for (i = 0; i < MAX_BEARERS; i++) {
560 if (!n_ptr->links[i])
561 continue;
562 tipc_printf(buf, "Links[%u]: %x, ", i, n_ptr->links[i]);
564 tipc_printf(buf, "Active links: [%x,%x]\n",
565 n_ptr->active_links[0], n_ptr->active_links[1]);
567 #endif
569 u32 tipc_available_nodes(const u32 domain)
571 struct node *n_ptr;
572 u32 cnt = 0;
574 for (n_ptr = tipc_nodes; n_ptr; n_ptr = n_ptr->next) {
575 if (!in_scope(domain, n_ptr->addr))
576 continue;
577 if (tipc_node_is_up(n_ptr))
578 cnt++;
580 return cnt;
583 struct sk_buff *tipc_node_get_nodes(const void *req_tlv_area, int req_tlv_space)
585 u32 domain;
586 struct sk_buff *buf;
587 struct node *n_ptr;
588 struct tipc_node_info node_info;
590 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NET_ADDR))
591 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
593 domain = *(u32 *)TLV_DATA(req_tlv_area);
594 domain = ntohl(domain);
595 if (!tipc_addr_domain_valid(domain))
596 return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
597 " (network address)");
599 if (!tipc_nodes)
600 return tipc_cfg_reply_none();
602 /* For now, get space for all other nodes
603 (will need to modify this when slave nodes are supported */
605 buf = tipc_cfg_reply_alloc(TLV_SPACE(sizeof(node_info)) *
606 (tipc_max_nodes - 1));
607 if (!buf)
608 return NULL;
610 /* Add TLVs for all nodes in scope */
612 for (n_ptr = tipc_nodes; n_ptr; n_ptr = n_ptr->next) {
613 if (!in_scope(domain, n_ptr->addr))
614 continue;
615 node_info.addr = htonl(n_ptr->addr);
616 node_info.up = htonl(tipc_node_is_up(n_ptr));
617 tipc_cfg_append_tlv(buf, TIPC_TLV_NODE_INFO,
618 &node_info, sizeof(node_info));
621 return buf;
624 struct sk_buff *tipc_node_get_links(const void *req_tlv_area, int req_tlv_space)
626 u32 domain;
627 struct sk_buff *buf;
628 struct node *n_ptr;
629 struct tipc_link_info link_info;
631 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NET_ADDR))
632 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
634 domain = *(u32 *)TLV_DATA(req_tlv_area);
635 domain = ntohl(domain);
636 if (!tipc_addr_domain_valid(domain))
637 return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
638 " (network address)");
640 if (!tipc_nodes)
641 return tipc_cfg_reply_none();
643 /* For now, get space for 2 links to all other nodes + bcast link
644 (will need to modify this when slave nodes are supported */
646 buf = tipc_cfg_reply_alloc(TLV_SPACE(sizeof(link_info)) *
647 (2 * (tipc_max_nodes - 1) + 1));
648 if (!buf)
649 return NULL;
651 /* Add TLV for broadcast link */
653 link_info.dest = tipc_own_addr & 0xfffff00;
654 link_info.dest = htonl(link_info.dest);
655 link_info.up = htonl(1);
656 sprintf(link_info.str, tipc_bclink_name);
657 tipc_cfg_append_tlv(buf, TIPC_TLV_LINK_INFO, &link_info, sizeof(link_info));
659 /* Add TLVs for any other links in scope */
661 for (n_ptr = tipc_nodes; n_ptr; n_ptr = n_ptr->next) {
662 u32 i;
664 if (!in_scope(domain, n_ptr->addr))
665 continue;
666 for (i = 0; i < MAX_BEARERS; i++) {
667 if (!n_ptr->links[i])
668 continue;
669 link_info.dest = htonl(n_ptr->addr);
670 link_info.up = htonl(tipc_link_is_up(n_ptr->links[i]));
671 strcpy(link_info.str, n_ptr->links[i]->name);
672 tipc_cfg_append_tlv(buf, TIPC_TLV_LINK_INFO,
673 &link_info, sizeof(link_info));
677 return buf;