name_distr.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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(INT_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, INT_H_SIZE, dest);
  92. msg_set_size(msg, INT_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. kfree_skb(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 nodearg)
  151. {
  152. struct tipc_node *n_ptr;
  153. struct tipc_link *l_ptr;
  154. struct publication *publ;
  155. struct distr_item *item = NULL;
  156. struct sk_buff *buf = NULL;
  157. struct list_head message_list;
  158. u32 node = (u32)nodearg;
  159. u32 left = 0;
  160. u32 rest;
  161. u32 max_item_buf = 0;
  162. /* compute maximum amount of publication data to send per message */
  163. read_lock_bh(&tipc_net_lock);
  164. n_ptr = tipc_node_find(node);
  165. if (n_ptr) {
  166. tipc_node_lock(n_ptr);
  167. l_ptr = n_ptr->active_links[0];
  168. if (l_ptr)
  169. max_item_buf = ((l_ptr->max_pkt - INT_H_SIZE) /
  170. ITEM_SIZE) * ITEM_SIZE;
  171. tipc_node_unlock(n_ptr);
  172. }
  173. read_unlock_bh(&tipc_net_lock);
  174. if (!max_item_buf)
  175. return;
  176. /* create list of publication messages, then send them as a unit */
  177. INIT_LIST_HEAD(&message_list);
  178. read_lock_bh(&tipc_nametbl_lock);
  179. rest = publ_cnt * ITEM_SIZE;
  180. list_for_each_entry(publ, &publ_root, local_list) {
  181. if (!buf) {
  182. left = (rest <= max_item_buf) ? rest : max_item_buf;
  183. rest -= left;
  184. buf = named_prepare_buf(PUBLICATION, left, node);
  185. if (!buf) {
  186. warn("Bulk publication distribution failure\n");
  187. goto exit;
  188. }
  189. item = (struct distr_item *)msg_data(buf_msg(buf));
  190. }
  191. publ_to_item(item, publ);
  192. item++;
  193. left -= ITEM_SIZE;
  194. if (!left) {
  195. list_add_tail((struct list_head *)buf, &message_list);
  196. buf = NULL;
  197. }
  198. }
  199. exit:
  200. read_unlock_bh(&tipc_nametbl_lock);
  201. tipc_link_send_names(&message_list, (u32)node);
  202. }
  203. /**
  204. * named_purge_publ - remove publication associated with a failed node
  205. *
  206. * Invoked for each publication issued by a newly failed node.
  207. * Removes publication structure from name table & deletes it.
  208. */
  209. static void named_purge_publ(struct publication *publ)
  210. {
  211. struct publication *p;
  212. write_lock_bh(&tipc_nametbl_lock);
  213. p = tipc_nametbl_remove_publ(publ->type, publ->lower,
  214. publ->node, publ->ref, publ->key);
  215. if (p)
  216. tipc_nodesub_unsubscribe(&p->subscr);
  217. write_unlock_bh(&tipc_nametbl_lock);
  218. if (p != publ) {
  219. err("Unable to remove publication from failed node\n"
  220. "(type=%u, lower=%u, node=0x%x, ref=%u, key=%u)\n",
  221. publ->type, publ->lower, publ->node, publ->ref, publ->key);
  222. }
  223. kfree(p);
  224. }
  225. /**
  226. * tipc_named_recv - process name table update message sent by another node
  227. */
  228. void tipc_named_recv(struct sk_buff *buf)
  229. {
  230. struct publication *publ;
  231. struct tipc_msg *msg = buf_msg(buf);
  232. struct distr_item *item = (struct distr_item *)msg_data(msg);
  233. u32 count = msg_data_sz(msg) / ITEM_SIZE;
  234. write_lock_bh(&tipc_nametbl_lock);
  235. while (count--) {
  236. if (msg_type(msg) == PUBLICATION) {
  237. publ = tipc_nametbl_insert_publ(ntohl(item->type),
  238. ntohl(item->lower),
  239. ntohl(item->upper),
  240. TIPC_CLUSTER_SCOPE,
  241. msg_orignode(msg),
  242. ntohl(item->ref),
  243. ntohl(item->key));
  244. if (publ) {
  245. tipc_nodesub_subscribe(&publ->subscr,
  246. msg_orignode(msg),
  247. publ,
  248. (net_ev_handler)
  249. named_purge_publ);
  250. }
  251. } else if (msg_type(msg) == WITHDRAWAL) {
  252. publ = tipc_nametbl_remove_publ(ntohl(item->type),
  253. ntohl(item->lower),
  254. msg_orignode(msg),
  255. ntohl(item->ref),
  256. ntohl(item->key));
  257. if (publ) {
  258. tipc_nodesub_unsubscribe(&publ->subscr);
  259. kfree(publ);
  260. } else {
  261. err("Unable to remove publication by node 0x%x\n"
  262. "(type=%u, lower=%u, ref=%u, key=%u)\n",
  263. msg_orignode(msg),
  264. ntohl(item->type), ntohl(item->lower),
  265. ntohl(item->ref), ntohl(item->key));
  266. }
  267. } else {
  268. warn("Unrecognized name table message received\n");
  269. }
  270. item++;
  271. }
  272. write_unlock_bh(&tipc_nametbl_lock);
  273. kfree_skb(buf);
  274. }
  275. /**
  276. * tipc_named_reinit - re-initialize local publication list
  277. *
  278. * This routine is called whenever TIPC networking is enabled.
  279. * All existing publications by this node that have "cluster" or "zone" scope
  280. * are updated to reflect the node's new network address.
  281. */
  282. void tipc_named_reinit(void)
  283. {
  284. struct publication *publ;
  285. write_lock_bh(&tipc_nametbl_lock);
  286. list_for_each_entry(publ, &publ_root, local_list)
  287. publ->node = tipc_own_addr;
  288. write_unlock_bh(&tipc_nametbl_lock);
  289. }