name_distr.c 9.2 KB

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