name_distr.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * net/tipc/name_distr.c: TIPC name distribution code
  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 "link.h"
  38. #include "name_distr.h"
  39. #define ITEM_SIZE sizeof(struct distr_item)
  40. /**
  41. * struct distr_item - publication info distributed to other nodes
  42. * @type: name sequence type
  43. * @lower: name sequence lower bound
  44. * @upper: name sequence upper bound
  45. * @ref: publishing port reference
  46. * @key: publication key
  47. *
  48. * ===> All fields are stored in network byte order. <===
  49. *
  50. * First 3 fields identify (name or) name sequence being published.
  51. * Reference field uniquely identifies port that published name sequence.
  52. * Key field uniquely identifies publication, in the event a port has
  53. * multiple publications of the same name sequence.
  54. *
  55. * Note: There is no field that identifies the publishing node because it is
  56. * the same for all items contained within a publication message.
  57. */
  58. struct distr_item {
  59. __be32 type;
  60. __be32 lower;
  61. __be32 upper;
  62. __be32 ref;
  63. __be32 key;
  64. };
  65. /**
  66. * List of externally visible publications by this node --
  67. * that is, all publications having scope > TIPC_NODE_SCOPE.
  68. */
  69. static LIST_HEAD(publ_root);
  70. static u32 publ_cnt;
  71. /**
  72. * publ_to_item - add publication info to a publication message
  73. */
  74. static void publ_to_item(struct distr_item *i, struct publication *p)
  75. {
  76. i->type = htonl(p->type);
  77. i->lower = htonl(p->lower);
  78. i->upper = htonl(p->upper);
  79. i->ref = htonl(p->ref);
  80. i->key = htonl(p->key);
  81. }
  82. /**
  83. * named_prepare_buf - allocate & initialize a publication message
  84. */
  85. static struct sk_buff *named_prepare_buf(u32 type, u32 size, u32 dest)
  86. {
  87. struct sk_buff *buf = tipc_buf_acquire(LONG_H_SIZE + size);
  88. struct tipc_msg *msg;
  89. if (buf != NULL) {
  90. msg = buf_msg(buf);
  91. tipc_msg_init(msg, NAME_DISTRIBUTOR, type, LONG_H_SIZE, dest);
  92. msg_set_size(msg, LONG_H_SIZE + size);
  93. }
  94. return buf;
  95. }
  96. static void named_cluster_distribute(struct sk_buff *buf)
  97. {
  98. struct sk_buff *buf_copy;
  99. struct tipc_node *n_ptr;
  100. u32 n_num;
  101. for (n_num = 1; n_num <= tipc_net.highest_node; n_num++) {
  102. n_ptr = tipc_net.nodes[n_num];
  103. if (n_ptr && tipc_node_has_active_links(n_ptr)) {
  104. buf_copy = skb_copy(buf, GFP_ATOMIC);
  105. if (!buf_copy)
  106. break;
  107. msg_set_destnode(buf_msg(buf_copy), n_ptr->addr);
  108. tipc_link_send(buf_copy, n_ptr->addr, n_ptr->addr);
  109. }
  110. }
  111. buf_discard(buf);
  112. }
  113. /**
  114. * tipc_named_publish - tell other nodes about a new publication by this node
  115. */
  116. void tipc_named_publish(struct publication *publ)
  117. {
  118. struct sk_buff *buf;
  119. struct distr_item *item;
  120. list_add_tail(&publ->local_list, &publ_root);
  121. publ_cnt++;
  122. buf = named_prepare_buf(PUBLICATION, ITEM_SIZE, 0);
  123. if (!buf) {
  124. warn("Publication distribution failure\n");
  125. return;
  126. }
  127. item = (struct distr_item *)msg_data(buf_msg(buf));
  128. publ_to_item(item, publ);
  129. named_cluster_distribute(buf);
  130. }
  131. /**
  132. * tipc_named_withdraw - tell other nodes about a withdrawn publication by this node
  133. */
  134. void tipc_named_withdraw(struct publication *publ)
  135. {
  136. struct sk_buff *buf;
  137. struct distr_item *item;
  138. list_del(&publ->local_list);
  139. publ_cnt--;
  140. buf = named_prepare_buf(WITHDRAWAL, ITEM_SIZE, 0);
  141. if (!buf) {
  142. warn("Withdrawl distribution failure\n");
  143. return;
  144. }
  145. item = (struct distr_item *)msg_data(buf_msg(buf));
  146. publ_to_item(item, publ);
  147. named_cluster_distribute(buf);
  148. }
  149. /**
  150. * tipc_named_node_up - tell specified node about all publications by this node
  151. */
  152. void tipc_named_node_up(unsigned long node)
  153. {
  154. struct publication *publ;
  155. struct distr_item *item = NULL;
  156. struct sk_buff *buf = NULL;
  157. u32 left = 0;
  158. u32 rest;
  159. u32 max_item_buf;
  160. read_lock_bh(&tipc_nametbl_lock);
  161. max_item_buf = TIPC_MAX_USER_MSG_SIZE / ITEM_SIZE;
  162. max_item_buf *= ITEM_SIZE;
  163. rest = publ_cnt * ITEM_SIZE;
  164. list_for_each_entry(publ, &publ_root, local_list) {
  165. if (!buf) {
  166. left = (rest <= max_item_buf) ? rest : max_item_buf;
  167. rest -= left;
  168. buf = named_prepare_buf(PUBLICATION, left, node);
  169. if (!buf) {
  170. warn("Bulk publication distribution failure\n");
  171. goto exit;
  172. }
  173. item = (struct distr_item *)msg_data(buf_msg(buf));
  174. }
  175. publ_to_item(item, publ);
  176. item++;
  177. left -= ITEM_SIZE;
  178. if (!left) {
  179. msg_set_link_selector(buf_msg(buf), node);
  180. tipc_link_send(buf, node, node);
  181. buf = NULL;
  182. }
  183. }
  184. exit:
  185. read_unlock_bh(&tipc_nametbl_lock);
  186. }
  187. /**
  188. * node_is_down - remove publication associated with a failed node
  189. *
  190. * Invoked for each publication issued by a newly failed node.
  191. * Removes publication structure from name table & deletes it.
  192. * In rare cases the link may have come back up again when this
  193. * function is called, and we have two items representing the same
  194. * publication. Nudge this item's key to distinguish it from the other.
  195. * (Note: Publication's node subscription is already unsubscribed.)
  196. */
  197. static void node_is_down(struct publication *publ)
  198. {
  199. struct publication *p;
  200. write_lock_bh(&tipc_nametbl_lock);
  201. publ->key += 1222345;
  202. p = tipc_nametbl_remove_publ(publ->type, publ->lower,
  203. publ->node, publ->ref, publ->key);
  204. write_unlock_bh(&tipc_nametbl_lock);
  205. if (p != publ) {
  206. err("Unable to remove publication from failed node\n"
  207. "(type=%u, lower=%u, node=0x%x, ref=%u, key=%u)\n",
  208. publ->type, publ->lower, publ->node, publ->ref, publ->key);
  209. }
  210. kfree(p);
  211. }
  212. /**
  213. * tipc_named_recv - process name table update message sent by another node
  214. */
  215. void tipc_named_recv(struct sk_buff *buf)
  216. {
  217. struct publication *publ;
  218. struct tipc_msg *msg = buf_msg(buf);
  219. struct distr_item *item = (struct distr_item *)msg_data(msg);
  220. u32 count = msg_data_sz(msg) / ITEM_SIZE;
  221. write_lock_bh(&tipc_nametbl_lock);
  222. while (count--) {
  223. if (msg_type(msg) == PUBLICATION) {
  224. publ = tipc_nametbl_insert_publ(ntohl(item->type),
  225. ntohl(item->lower),
  226. ntohl(item->upper),
  227. TIPC_CLUSTER_SCOPE,
  228. msg_orignode(msg),
  229. ntohl(item->ref),
  230. ntohl(item->key));
  231. if (publ) {
  232. tipc_nodesub_subscribe(&publ->subscr,
  233. msg_orignode(msg),
  234. publ,
  235. (net_ev_handler)node_is_down);
  236. }
  237. } else if (msg_type(msg) == WITHDRAWAL) {
  238. publ = tipc_nametbl_remove_publ(ntohl(item->type),
  239. ntohl(item->lower),
  240. msg_orignode(msg),
  241. ntohl(item->ref),
  242. ntohl(item->key));
  243. if (publ) {
  244. tipc_nodesub_unsubscribe(&publ->subscr);
  245. kfree(publ);
  246. } else {
  247. err("Unable to remove publication by node 0x%x\n"
  248. "(type=%u, lower=%u, ref=%u, key=%u)\n",
  249. msg_orignode(msg),
  250. ntohl(item->type), ntohl(item->lower),
  251. ntohl(item->ref), ntohl(item->key));
  252. }
  253. } else {
  254. warn("Unrecognized name table message received\n");
  255. }
  256. item++;
  257. }
  258. write_unlock_bh(&tipc_nametbl_lock);
  259. buf_discard(buf);
  260. }
  261. /**
  262. * tipc_named_reinit - re-initialize local publication list
  263. *
  264. * This routine is called whenever TIPC networking is (re)enabled.
  265. * All existing publications by this node that have "cluster" or "zone" scope
  266. * are updated to reflect the node's current network address.
  267. * (If the node's address is unchanged, the update loop terminates immediately.)
  268. */
  269. void tipc_named_reinit(void)
  270. {
  271. struct publication *publ;
  272. write_lock_bh(&tipc_nametbl_lock);
  273. list_for_each_entry(publ, &publ_root, local_list) {
  274. if (publ->node == tipc_own_addr)
  275. break;
  276. publ->node = tipc_own_addr;
  277. }
  278. write_unlock_bh(&tipc_nametbl_lock);
  279. }