name_distr.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. * struct publ_list - list of publications made by this node
  67. * @list: circular list of publications
  68. * @list_size: number of entries in list
  69. */
  70. struct publ_list {
  71. struct list_head list;
  72. u32 size;
  73. };
  74. static struct publ_list publ_zone = {
  75. .list = LIST_HEAD_INIT(publ_zone.list),
  76. .size = 0,
  77. };
  78. static struct publ_list publ_cluster = {
  79. .list = LIST_HEAD_INIT(publ_cluster.list),
  80. .size = 0,
  81. };
  82. static struct publ_list publ_node = {
  83. .list = LIST_HEAD_INIT(publ_node.list),
  84. .size = 0,
  85. };
  86. static struct publ_list *publ_lists[] = {
  87. NULL,
  88. &publ_zone, /* publ_lists[TIPC_ZONE_SCOPE] */
  89. &publ_cluster, /* publ_lists[TIPC_CLUSTER_SCOPE] */
  90. &publ_node /* publ_lists[TIPC_NODE_SCOPE] */
  91. };
  92. /**
  93. * publ_to_item - add publication info to a publication message
  94. */
  95. static void publ_to_item(struct distr_item *i, struct publication *p)
  96. {
  97. i->type = htonl(p->type);
  98. i->lower = htonl(p->lower);
  99. i->upper = htonl(p->upper);
  100. i->ref = htonl(p->ref);
  101. i->key = htonl(p->key);
  102. }
  103. /**
  104. * named_prepare_buf - allocate & initialize a publication message
  105. */
  106. static struct sk_buff *named_prepare_buf(u32 type, u32 size, u32 dest)
  107. {
  108. struct sk_buff *buf = tipc_buf_acquire(INT_H_SIZE + size);
  109. struct tipc_msg *msg;
  110. if (buf != NULL) {
  111. msg = buf_msg(buf);
  112. tipc_msg_init(msg, NAME_DISTRIBUTOR, type, INT_H_SIZE, dest);
  113. msg_set_size(msg, INT_H_SIZE + size);
  114. }
  115. return buf;
  116. }
  117. static void named_cluster_distribute(struct sk_buff *buf)
  118. {
  119. struct sk_buff *buf_copy;
  120. struct tipc_node *n_ptr;
  121. struct tipc_link *l_ptr;
  122. rcu_read_lock();
  123. list_for_each_entry_rcu(n_ptr, &tipc_node_list, list) {
  124. spin_lock_bh(&n_ptr->lock);
  125. l_ptr = n_ptr->active_links[n_ptr->addr & 1];
  126. if (l_ptr) {
  127. buf_copy = skb_copy(buf, GFP_ATOMIC);
  128. if (!buf_copy) {
  129. spin_unlock_bh(&n_ptr->lock);
  130. break;
  131. }
  132. msg_set_destnode(buf_msg(buf_copy), n_ptr->addr);
  133. __tipc_link_xmit(l_ptr, buf_copy);
  134. }
  135. spin_unlock_bh(&n_ptr->lock);
  136. }
  137. rcu_read_unlock();
  138. kfree_skb(buf);
  139. }
  140. /**
  141. * tipc_named_publish - tell other nodes about a new publication by this node
  142. */
  143. void tipc_named_publish(struct publication *publ)
  144. {
  145. struct sk_buff *buf;
  146. struct distr_item *item;
  147. list_add_tail(&publ->local_list, &publ_lists[publ->scope]->list);
  148. publ_lists[publ->scope]->size++;
  149. if (publ->scope == TIPC_NODE_SCOPE)
  150. return;
  151. buf = named_prepare_buf(PUBLICATION, ITEM_SIZE, 0);
  152. if (!buf) {
  153. pr_warn("Publication distribution failure\n");
  154. return;
  155. }
  156. item = (struct distr_item *)msg_data(buf_msg(buf));
  157. publ_to_item(item, publ);
  158. named_cluster_distribute(buf);
  159. }
  160. /**
  161. * tipc_named_withdraw - tell other nodes about a withdrawn publication by this node
  162. */
  163. void tipc_named_withdraw(struct publication *publ)
  164. {
  165. struct sk_buff *buf;
  166. struct distr_item *item;
  167. list_del(&publ->local_list);
  168. publ_lists[publ->scope]->size--;
  169. if (publ->scope == TIPC_NODE_SCOPE)
  170. return;
  171. buf = named_prepare_buf(WITHDRAWAL, ITEM_SIZE, 0);
  172. if (!buf) {
  173. pr_warn("Withdrawal distribution failure\n");
  174. return;
  175. }
  176. item = (struct distr_item *)msg_data(buf_msg(buf));
  177. publ_to_item(item, publ);
  178. named_cluster_distribute(buf);
  179. }
  180. /*
  181. * named_distribute - prepare name info for bulk distribution to another node
  182. */
  183. static void named_distribute(struct list_head *message_list, u32 node,
  184. struct publ_list *pls, u32 max_item_buf)
  185. {
  186. struct publication *publ;
  187. struct sk_buff *buf = NULL;
  188. struct distr_item *item = NULL;
  189. u32 left = 0;
  190. u32 rest = pls->size * ITEM_SIZE;
  191. list_for_each_entry(publ, &pls->list, local_list) {
  192. if (!buf) {
  193. left = (rest <= max_item_buf) ? rest : max_item_buf;
  194. rest -= left;
  195. buf = named_prepare_buf(PUBLICATION, left, node);
  196. if (!buf) {
  197. pr_warn("Bulk publication failure\n");
  198. return;
  199. }
  200. item = (struct distr_item *)msg_data(buf_msg(buf));
  201. }
  202. publ_to_item(item, publ);
  203. item++;
  204. left -= ITEM_SIZE;
  205. if (!left) {
  206. list_add_tail((struct list_head *)buf, message_list);
  207. buf = NULL;
  208. }
  209. }
  210. }
  211. /**
  212. * tipc_named_node_up - tell specified node about all publications by this node
  213. */
  214. void tipc_named_node_up(unsigned long nodearg)
  215. {
  216. struct tipc_node *n_ptr;
  217. struct tipc_link *l_ptr;
  218. struct list_head message_list;
  219. u32 node = (u32)nodearg;
  220. u32 max_item_buf = 0;
  221. /* compute maximum amount of publication data to send per message */
  222. read_lock_bh(&tipc_net_lock);
  223. n_ptr = tipc_node_find(node);
  224. if (n_ptr) {
  225. tipc_node_lock(n_ptr);
  226. l_ptr = n_ptr->active_links[0];
  227. if (l_ptr)
  228. max_item_buf = ((l_ptr->max_pkt - INT_H_SIZE) /
  229. ITEM_SIZE) * ITEM_SIZE;
  230. tipc_node_unlock(n_ptr);
  231. }
  232. read_unlock_bh(&tipc_net_lock);
  233. if (!max_item_buf)
  234. return;
  235. /* create list of publication messages, then send them as a unit */
  236. INIT_LIST_HEAD(&message_list);
  237. read_lock_bh(&tipc_nametbl_lock);
  238. named_distribute(&message_list, node, &publ_cluster, max_item_buf);
  239. named_distribute(&message_list, node, &publ_zone, max_item_buf);
  240. read_unlock_bh(&tipc_nametbl_lock);
  241. tipc_link_names_xmit(&message_list, node);
  242. }
  243. /**
  244. * named_purge_publ - remove publication associated with a failed node
  245. *
  246. * Invoked for each publication issued by a newly failed node.
  247. * Removes publication structure from name table & deletes it.
  248. */
  249. static void named_purge_publ(struct publication *publ)
  250. {
  251. struct publication *p;
  252. write_lock_bh(&tipc_nametbl_lock);
  253. p = tipc_nametbl_remove_publ(publ->type, publ->lower,
  254. publ->node, publ->ref, publ->key);
  255. if (p)
  256. tipc_nodesub_unsubscribe(&p->subscr);
  257. write_unlock_bh(&tipc_nametbl_lock);
  258. if (p != publ) {
  259. pr_err("Unable to remove publication from failed node\n"
  260. " (type=%u, lower=%u, node=0x%x, ref=%u, key=%u)\n",
  261. publ->type, publ->lower, publ->node, publ->ref,
  262. publ->key);
  263. }
  264. kfree(p);
  265. }
  266. /**
  267. * tipc_named_rcv - process name table update message sent by another node
  268. */
  269. void tipc_named_rcv(struct sk_buff *buf)
  270. {
  271. struct publication *publ;
  272. struct tipc_msg *msg = buf_msg(buf);
  273. struct distr_item *item = (struct distr_item *)msg_data(msg);
  274. u32 count = msg_data_sz(msg) / ITEM_SIZE;
  275. write_lock_bh(&tipc_nametbl_lock);
  276. while (count--) {
  277. if (msg_type(msg) == PUBLICATION) {
  278. publ = tipc_nametbl_insert_publ(ntohl(item->type),
  279. ntohl(item->lower),
  280. ntohl(item->upper),
  281. TIPC_CLUSTER_SCOPE,
  282. msg_orignode(msg),
  283. ntohl(item->ref),
  284. ntohl(item->key));
  285. if (publ) {
  286. tipc_nodesub_subscribe(&publ->subscr,
  287. msg_orignode(msg),
  288. publ,
  289. (net_ev_handler)
  290. named_purge_publ);
  291. }
  292. } else if (msg_type(msg) == WITHDRAWAL) {
  293. publ = tipc_nametbl_remove_publ(ntohl(item->type),
  294. ntohl(item->lower),
  295. msg_orignode(msg),
  296. ntohl(item->ref),
  297. ntohl(item->key));
  298. if (publ) {
  299. tipc_nodesub_unsubscribe(&publ->subscr);
  300. kfree(publ);
  301. } else {
  302. pr_err("Unable to remove publication by node 0x%x\n"
  303. " (type=%u, lower=%u, ref=%u, key=%u)\n",
  304. msg_orignode(msg), ntohl(item->type),
  305. ntohl(item->lower), ntohl(item->ref),
  306. ntohl(item->key));
  307. }
  308. } else {
  309. pr_warn("Unrecognized name table message received\n");
  310. }
  311. item++;
  312. }
  313. write_unlock_bh(&tipc_nametbl_lock);
  314. kfree_skb(buf);
  315. }
  316. /**
  317. * tipc_named_reinit - re-initialize local publications
  318. *
  319. * This routine is called whenever TIPC networking is enabled.
  320. * All name table entries published by this node are updated to reflect
  321. * the node's new network address.
  322. */
  323. void tipc_named_reinit(void)
  324. {
  325. struct publication *publ;
  326. int scope;
  327. write_lock_bh(&tipc_nametbl_lock);
  328. for (scope = TIPC_ZONE_SCOPE; scope <= TIPC_NODE_SCOPE; scope++)
  329. list_for_each_entry(publ, &publ_lists[scope]->list, local_list)
  330. publ->node = tipc_own_addr;
  331. write_unlock_bh(&tipc_nametbl_lock);
  332. }