name_distr.c 9.2 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. /**
  40. * struct publ_list - list of publications made by this node
  41. * @list: circular list of publications
  42. * @list_size: number of entries in list
  43. */
  44. struct publ_list {
  45. struct list_head list;
  46. u32 size;
  47. };
  48. static struct publ_list publ_zone = {
  49. .list = LIST_HEAD_INIT(publ_zone.list),
  50. .size = 0,
  51. };
  52. static struct publ_list publ_cluster = {
  53. .list = LIST_HEAD_INIT(publ_cluster.list),
  54. .size = 0,
  55. };
  56. static struct publ_list publ_node = {
  57. .list = LIST_HEAD_INIT(publ_node.list),
  58. .size = 0,
  59. };
  60. static struct publ_list *publ_lists[] = {
  61. NULL,
  62. &publ_zone, /* publ_lists[TIPC_ZONE_SCOPE] */
  63. &publ_cluster, /* publ_lists[TIPC_CLUSTER_SCOPE] */
  64. &publ_node /* publ_lists[TIPC_NODE_SCOPE] */
  65. };
  66. /**
  67. * publ_to_item - add publication info to a publication message
  68. */
  69. static void publ_to_item(struct distr_item *i, struct publication *p)
  70. {
  71. i->type = htonl(p->type);
  72. i->lower = htonl(p->lower);
  73. i->upper = htonl(p->upper);
  74. i->ref = htonl(p->ref);
  75. i->key = htonl(p->key);
  76. }
  77. /**
  78. * named_prepare_buf - allocate & initialize a publication message
  79. */
  80. static struct sk_buff *named_prepare_buf(u32 type, u32 size, u32 dest)
  81. {
  82. struct sk_buff *buf = tipc_buf_acquire(INT_H_SIZE + size);
  83. struct tipc_msg *msg;
  84. if (buf != NULL) {
  85. msg = buf_msg(buf);
  86. tipc_msg_init(msg, NAME_DISTRIBUTOR, type, INT_H_SIZE, dest);
  87. msg_set_size(msg, INT_H_SIZE + size);
  88. }
  89. return buf;
  90. }
  91. void named_cluster_distribute(struct sk_buff *buf)
  92. {
  93. struct sk_buff *obuf;
  94. struct tipc_node *node;
  95. u32 dnode;
  96. rcu_read_lock();
  97. list_for_each_entry_rcu(node, &tipc_node_list, list) {
  98. dnode = node->addr;
  99. if (in_own_node(dnode))
  100. continue;
  101. if (!tipc_node_active_links(node))
  102. continue;
  103. obuf = skb_copy(buf, GFP_ATOMIC);
  104. if (!obuf)
  105. break;
  106. msg_set_destnode(buf_msg(obuf), dnode);
  107. tipc_link_xmit(obuf, dnode, dnode);
  108. }
  109. rcu_read_unlock();
  110. kfree_skb(buf);
  111. }
  112. /**
  113. * tipc_named_publish - tell other nodes about a new publication by this node
  114. */
  115. struct sk_buff *tipc_named_publish(struct publication *publ)
  116. {
  117. struct sk_buff *buf;
  118. struct distr_item *item;
  119. list_add_tail(&publ->local_list, &publ_lists[publ->scope]->list);
  120. publ_lists[publ->scope]->size++;
  121. if (publ->scope == TIPC_NODE_SCOPE)
  122. return NULL;
  123. buf = named_prepare_buf(PUBLICATION, ITEM_SIZE, 0);
  124. if (!buf) {
  125. pr_warn("Publication distribution failure\n");
  126. return NULL;
  127. }
  128. item = (struct distr_item *)msg_data(buf_msg(buf));
  129. publ_to_item(item, publ);
  130. return buf;
  131. }
  132. /**
  133. * tipc_named_withdraw - tell other nodes about a withdrawn publication by this node
  134. */
  135. struct sk_buff *tipc_named_withdraw(struct publication *publ)
  136. {
  137. struct sk_buff *buf;
  138. struct distr_item *item;
  139. list_del(&publ->local_list);
  140. publ_lists[publ->scope]->size--;
  141. if (publ->scope == TIPC_NODE_SCOPE)
  142. return NULL;
  143. buf = named_prepare_buf(WITHDRAWAL, ITEM_SIZE, 0);
  144. if (!buf) {
  145. pr_warn("Withdrawal distribution failure\n");
  146. return NULL;
  147. }
  148. item = (struct distr_item *)msg_data(buf_msg(buf));
  149. publ_to_item(item, publ);
  150. return buf;
  151. }
  152. /**
  153. * named_distribute - prepare name info for bulk distribution to another node
  154. * @msg_list: list of messages (buffers) to be returned from this function
  155. * @dnode: node to be updated
  156. * @pls: linked list of publication items to be packed into buffer chain
  157. */
  158. static void named_distribute(struct list_head *msg_list, u32 dnode,
  159. struct publ_list *pls)
  160. {
  161. struct publication *publ;
  162. struct sk_buff *buf = NULL;
  163. struct distr_item *item = NULL;
  164. uint dsz = pls->size * ITEM_SIZE;
  165. uint msg_dsz = (tipc_node_get_mtu(dnode, 0) / ITEM_SIZE) * ITEM_SIZE;
  166. uint rem = dsz;
  167. uint msg_rem = 0;
  168. list_for_each_entry(publ, &pls->list, local_list) {
  169. /* Prepare next buffer: */
  170. if (!buf) {
  171. msg_rem = min_t(uint, rem, msg_dsz);
  172. rem -= msg_rem;
  173. buf = named_prepare_buf(PUBLICATION, msg_rem, dnode);
  174. if (!buf) {
  175. pr_warn("Bulk publication failure\n");
  176. return;
  177. }
  178. item = (struct distr_item *)msg_data(buf_msg(buf));
  179. }
  180. /* Pack publication into message: */
  181. publ_to_item(item, publ);
  182. item++;
  183. msg_rem -= ITEM_SIZE;
  184. /* Append full buffer to list: */
  185. if (!msg_rem) {
  186. list_add_tail((struct list_head *)buf, msg_list);
  187. buf = NULL;
  188. }
  189. }
  190. }
  191. /**
  192. * tipc_named_node_up - tell specified node about all publications by this node
  193. */
  194. void tipc_named_node_up(u32 dnode)
  195. {
  196. LIST_HEAD(msg_list);
  197. struct sk_buff *buf_chain;
  198. read_lock_bh(&tipc_nametbl_lock);
  199. named_distribute(&msg_list, dnode, &publ_cluster);
  200. named_distribute(&msg_list, dnode, &publ_zone);
  201. read_unlock_bh(&tipc_nametbl_lock);
  202. /* Convert circular list to linear list and send: */
  203. buf_chain = (struct sk_buff *)msg_list.next;
  204. ((struct sk_buff *)msg_list.prev)->next = NULL;
  205. tipc_link_xmit(buf_chain, dnode, dnode);
  206. }
  207. /**
  208. * named_purge_publ - remove publication associated with a failed node
  209. *
  210. * Invoked for each publication issued by a newly failed node.
  211. * Removes publication structure from name table & deletes it.
  212. */
  213. static void named_purge_publ(struct publication *publ)
  214. {
  215. struct publication *p;
  216. write_lock_bh(&tipc_nametbl_lock);
  217. p = tipc_nametbl_remove_publ(publ->type, publ->lower,
  218. publ->node, publ->ref, publ->key);
  219. if (p)
  220. tipc_nodesub_unsubscribe(&p->subscr);
  221. write_unlock_bh(&tipc_nametbl_lock);
  222. if (p != publ) {
  223. pr_err("Unable to remove publication from failed node\n"
  224. " (type=%u, lower=%u, node=0x%x, ref=%u, key=%u)\n",
  225. publ->type, publ->lower, publ->node, publ->ref,
  226. publ->key);
  227. }
  228. kfree(p);
  229. }
  230. /**
  231. * tipc_named_rcv - process name table update message sent by another node
  232. */
  233. void tipc_named_rcv(struct sk_buff *buf)
  234. {
  235. struct publication *publ;
  236. struct tipc_msg *msg = buf_msg(buf);
  237. struct distr_item *item = (struct distr_item *)msg_data(msg);
  238. u32 count = msg_data_sz(msg) / ITEM_SIZE;
  239. write_lock_bh(&tipc_nametbl_lock);
  240. while (count--) {
  241. if (msg_type(msg) == PUBLICATION) {
  242. publ = tipc_nametbl_insert_publ(ntohl(item->type),
  243. ntohl(item->lower),
  244. ntohl(item->upper),
  245. TIPC_CLUSTER_SCOPE,
  246. msg_orignode(msg),
  247. ntohl(item->ref),
  248. ntohl(item->key));
  249. if (publ) {
  250. tipc_nodesub_subscribe(&publ->subscr,
  251. msg_orignode(msg),
  252. publ,
  253. (net_ev_handler)
  254. named_purge_publ);
  255. }
  256. } else if (msg_type(msg) == WITHDRAWAL) {
  257. publ = tipc_nametbl_remove_publ(ntohl(item->type),
  258. ntohl(item->lower),
  259. msg_orignode(msg),
  260. ntohl(item->ref),
  261. ntohl(item->key));
  262. if (publ) {
  263. tipc_nodesub_unsubscribe(&publ->subscr);
  264. kfree(publ);
  265. } else {
  266. pr_err("Unable to remove publication by node 0x%x\n"
  267. " (type=%u, lower=%u, ref=%u, key=%u)\n",
  268. msg_orignode(msg), ntohl(item->type),
  269. ntohl(item->lower), ntohl(item->ref),
  270. ntohl(item->key));
  271. }
  272. } else {
  273. pr_warn("Unrecognized name table message received\n");
  274. }
  275. item++;
  276. }
  277. write_unlock_bh(&tipc_nametbl_lock);
  278. kfree_skb(buf);
  279. }
  280. /**
  281. * tipc_named_reinit - re-initialize local publications
  282. *
  283. * This routine is called whenever TIPC networking is enabled.
  284. * All name table entries published by this node are updated to reflect
  285. * the node's new network address.
  286. */
  287. void tipc_named_reinit(void)
  288. {
  289. struct publication *publ;
  290. int scope;
  291. write_lock_bh(&tipc_nametbl_lock);
  292. for (scope = TIPC_ZONE_SCOPE; scope <= TIPC_NODE_SCOPE; scope++)
  293. list_for_each_entry(publ, &publ_lists[scope]->list, local_list)
  294. publ->node = tipc_own_addr;
  295. write_unlock_bh(&tipc_nametbl_lock);
  296. }