name_distr.c 9.1 KB

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