cluster.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * net/tipc/cluster.c: TIPC cluster management routines
  3. *
  4. * Copyright (c) 2000-2006, Ericsson AB
  5. * Copyright (c) 2005, Wind River Systems
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. *
  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.
  19. *
  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.
  23. *
  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.
  35. */
  36. #include "core.h"
  37. #include "cluster.h"
  38. #include "link.h"
  39. struct tipc_node **tipc_local_nodes = NULL;
  40. struct tipc_node_map tipc_cltr_bcast_nodes = {0,{0,}};
  41. struct cluster *tipc_cltr_create(u32 addr)
  42. {
  43. struct cluster *c_ptr;
  44. int max_nodes;
  45. c_ptr = kzalloc(sizeof(*c_ptr), GFP_ATOMIC);
  46. if (c_ptr == NULL) {
  47. warn("Cluster creation failure, no memory\n");
  48. return NULL;
  49. }
  50. c_ptr->addr = tipc_addr(tipc_zone(addr), tipc_cluster(addr), 0);
  51. max_nodes = tipc_max_nodes + 1;
  52. c_ptr->nodes = kcalloc(max_nodes + 1, sizeof(void*), GFP_ATOMIC);
  53. if (c_ptr->nodes == NULL) {
  54. warn("Cluster creation failure, no memory for node area\n");
  55. kfree(c_ptr);
  56. return NULL;
  57. }
  58. tipc_local_nodes = c_ptr->nodes;
  59. c_ptr->highest_node = 0;
  60. tipc_net.clusters[1] = c_ptr;
  61. return c_ptr;
  62. }
  63. void tipc_cltr_delete(struct cluster *c_ptr)
  64. {
  65. u32 n_num;
  66. if (!c_ptr)
  67. return;
  68. for (n_num = 1; n_num <= c_ptr->highest_node; n_num++) {
  69. tipc_node_delete(c_ptr->nodes[n_num]);
  70. }
  71. kfree(c_ptr->nodes);
  72. kfree(c_ptr);
  73. }
  74. void tipc_cltr_attach_node(struct cluster *c_ptr, struct tipc_node *n_ptr)
  75. {
  76. u32 n_num = tipc_node(n_ptr->addr);
  77. u32 max_n_num = tipc_max_nodes;
  78. assert(n_num > 0);
  79. assert(n_num <= max_n_num);
  80. assert(c_ptr->nodes[n_num] == NULL);
  81. c_ptr->nodes[n_num] = n_ptr;
  82. if (n_num > c_ptr->highest_node)
  83. c_ptr->highest_node = n_num;
  84. }
  85. /**
  86. * tipc_cltr_broadcast - broadcast message to all nodes within cluster
  87. */
  88. void tipc_cltr_broadcast(struct sk_buff *buf)
  89. {
  90. struct sk_buff *buf_copy;
  91. struct cluster *c_ptr;
  92. struct tipc_node *n_ptr;
  93. u32 n_num;
  94. if (tipc_mode == TIPC_NET_MODE) {
  95. c_ptr = tipc_cltr_find(tipc_own_addr);
  96. /* Send to nodes */
  97. for (n_num = 1; n_num <= c_ptr->highest_node; n_num++) {
  98. n_ptr = c_ptr->nodes[n_num];
  99. if (n_ptr && tipc_node_has_active_links(n_ptr)) {
  100. buf_copy = skb_copy(buf, GFP_ATOMIC);
  101. if (buf_copy == NULL)
  102. goto exit;
  103. msg_set_destnode(buf_msg(buf_copy),
  104. n_ptr->addr);
  105. tipc_link_send(buf_copy, n_ptr->addr,
  106. n_ptr->addr);
  107. }
  108. }
  109. }
  110. exit:
  111. buf_discard(buf);
  112. }
  113. int tipc_cltr_init(void)
  114. {
  115. return tipc_cltr_create(tipc_own_addr) ? 0 : -ENOMEM;
  116. }