name_distr.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * net/tipc/name_distr.c: TIPC name distribution code
  3. *
  4. * Copyright (c) 2000-2006, Ericsson AB
  5. * Copyright (c) 2005, 2010-2011, 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. list_for_each_entry(n_ptr, &tipc_node_list, list) {
  101. if (tipc_node_active_links(n_ptr)) {
  102. buf_copy = skb_copy(buf, GFP_ATOMIC);
  103. if (!buf_copy)
  104. break;
  105. msg_set_destnode(buf_msg(buf_copy), n_ptr->addr);
  106. tipc_link_send(buf_copy, n_ptr->addr, n_ptr->addr);
  107. }
  108. }
  109. buf_discard(buf);
  110. }
  111. /**
  112. * tipc_named_publish - tell other nodes about a new publication by this node
  113. */
  114. void tipc_named_publish(struct publication *publ)
  115. {
  116. struct sk_buff *buf;
  117. struct distr_item *item;
  118. list_add_tail(&publ->local_list, &publ_root);
  119. publ_cnt++;
  120. buf = named_prepare_buf(PUBLICATION, ITEM_SIZE, 0);
  121. if (!buf) {
  122. warn("Publication distribution failure\n");
  123. return;
  124. }
  125. item = (struct distr_item *)msg_data(buf_msg(buf));
  126. publ_to_item(item, publ);
  127. named_cluster_distribute(buf);
  128. }
  129. /**
  130. * tipc_named_withdraw - tell other nodes about a withdrawn publication by this node
  131. */
  132. void tipc_named_withdraw(struct publication *publ)
  133. {
  134. struct sk_buff *buf;
  135. struct distr_item *item;
  136. list_del(&publ->local_list);
  137. publ_cnt--;
  138. buf = named_prepare_buf(WITHDRAWAL, ITEM_SIZE, 0);
  139. if (!buf) {
  140. warn("Withdrawal distribution failure\n");
  141. return;
  142. }
  143. item = (struct distr_item *)msg_data(buf_msg(buf));
  144. publ_to_item(item, publ);
  145. named_cluster_distribute(buf);
  146. }
  147. /**
  148. * tipc_named_node_up - tell specified node about all publications by this node
  149. */
  150. void tipc_named_node_up(unsigned long node)
  151. {
  152. struct publication *publ;
  153. struct distr_item *item = NULL;
  154. struct sk_buff *buf = NULL;
  155. u32 left = 0;
  156. u32 rest;
  157. u32 max_item_buf;
  158. read_lock_bh(&tipc_nametbl_lock);
  159. max_item_buf = TIPC_MAX_USER_MSG_SIZE / ITEM_SIZE;
  160. max_item_buf *= ITEM_SIZE;
  161. rest = publ_cnt * ITEM_SIZE;
  162. list_for_each_entry(publ, &publ_root, local_list) {
  163. if (!buf) {
  164. left = (rest <= max_item_buf) ? rest : max_item_buf;
  165. rest -= left;
  166. buf = named_prepare_buf(PUBLICATION, left, node);
  167. if (!buf) {
  168. warn("Bulk publication distribution failure\n");
  169. goto exit;
  170. }
  171. item = (struct distr_item *)msg_data(buf_msg(buf));
  172. }
  173. publ_to_item(item, publ);
  174. item++;
  175. left -= ITEM_SIZE;
  176. if (!left) {
  177. msg_set_link_selector(buf_msg(buf), node);
  178. tipc_link_send(buf, node, node);
  179. buf = NULL;
  180. }
  181. }
  182. exit:
  183. read_unlock_bh(&tipc_nametbl_lock);
  184. }
  185. /**
  186. * named_purge_publ - remove publication associated with a failed node
  187. *
  188. * Invoked for each publication issued by a newly failed node.
  189. * Removes publication structure from name table & deletes it.
  190. * In rare cases the link may have come back up again when this
  191. * function is called, and we have two items representing the same
  192. * publication. Nudge this item's key to distinguish it from the other.
  193. */
  194. static void named_purge_publ(struct publication *publ)
  195. {
  196. struct publication *p;
  197. write_lock_bh(&tipc_nametbl_lock);
  198. publ->key += 1222345;
  199. p = tipc_nametbl_remove_publ(publ->type, publ->lower,
  200. publ->node, publ->ref, publ->key);
  201. if (p)
  202. tipc_nodesub_unsubscribe(&p->subscr);
  203. write_unlock_bh(&tipc_nametbl_lock);
  204. if (p != publ) {
  205. err("Unable to remove publication from failed node\n"
  206. "(type=%u, lower=%u, node=0x%x, ref=%u, key=%u)\n",
  207. publ->type, publ->lower, publ->node, publ->ref, publ->key);
  208. }
  209. kfree(p);
  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. 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)
  235. named_purge_publ);
  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. }