name_distr.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 *buf_copy;
  94. struct tipc_node *n_ptr;
  95. struct tipc_link *l_ptr;
  96. rcu_read_lock();
  97. list_for_each_entry_rcu(n_ptr, &tipc_node_list, list) {
  98. tipc_node_lock(n_ptr);
  99. l_ptr = n_ptr->active_links[n_ptr->addr & 1];
  100. if (l_ptr) {
  101. buf_copy = skb_copy(buf, GFP_ATOMIC);
  102. if (!buf_copy) {
  103. tipc_node_unlock(n_ptr);
  104. break;
  105. }
  106. msg_set_destnode(buf_msg(buf_copy), n_ptr->addr);
  107. __tipc_link_xmit(l_ptr, buf_copy);
  108. }
  109. tipc_node_unlock(n_ptr);
  110. }
  111. rcu_read_unlock();
  112. kfree_skb(buf);
  113. }
  114. /**
  115. * tipc_named_publish - tell other nodes about a new publication by this node
  116. */
  117. struct sk_buff *tipc_named_publish(struct publication *publ)
  118. {
  119. struct sk_buff *buf;
  120. struct distr_item *item;
  121. list_add_tail(&publ->local_list, &publ_lists[publ->scope]->list);
  122. publ_lists[publ->scope]->size++;
  123. if (publ->scope == TIPC_NODE_SCOPE)
  124. return NULL;
  125. buf = named_prepare_buf(PUBLICATION, ITEM_SIZE, 0);
  126. if (!buf) {
  127. pr_warn("Publication distribution failure\n");
  128. return NULL;
  129. }
  130. item = (struct distr_item *)msg_data(buf_msg(buf));
  131. publ_to_item(item, publ);
  132. return buf;
  133. }
  134. /**
  135. * tipc_named_withdraw - tell other nodes about a withdrawn publication by this node
  136. */
  137. struct sk_buff *tipc_named_withdraw(struct publication *publ)
  138. {
  139. struct sk_buff *buf;
  140. struct distr_item *item;
  141. list_del(&publ->local_list);
  142. publ_lists[publ->scope]->size--;
  143. if (publ->scope == TIPC_NODE_SCOPE)
  144. return NULL;
  145. buf = named_prepare_buf(WITHDRAWAL, ITEM_SIZE, 0);
  146. if (!buf) {
  147. pr_warn("Withdrawal distribution failure\n");
  148. return NULL;
  149. }
  150. item = (struct distr_item *)msg_data(buf_msg(buf));
  151. publ_to_item(item, publ);
  152. return buf;
  153. }
  154. /*
  155. * named_distribute - prepare name info for bulk distribution to another node
  156. */
  157. static void named_distribute(struct list_head *message_list, u32 node,
  158. struct publ_list *pls, u32 max_item_buf)
  159. {
  160. struct publication *publ;
  161. struct sk_buff *buf = NULL;
  162. struct distr_item *item = NULL;
  163. u32 left = 0;
  164. u32 rest = pls->size * ITEM_SIZE;
  165. list_for_each_entry(publ, &pls->list, local_list) {
  166. if (!buf) {
  167. left = (rest <= max_item_buf) ? rest : max_item_buf;
  168. rest -= left;
  169. buf = named_prepare_buf(PUBLICATION, left, node);
  170. if (!buf) {
  171. pr_warn("Bulk publication failure\n");
  172. return;
  173. }
  174. item = (struct distr_item *)msg_data(buf_msg(buf));
  175. }
  176. publ_to_item(item, publ);
  177. item++;
  178. left -= ITEM_SIZE;
  179. if (!left) {
  180. list_add_tail((struct list_head *)buf, message_list);
  181. buf = NULL;
  182. }
  183. }
  184. }
  185. /**
  186. * tipc_named_node_up - tell specified node about all publications by this node
  187. */
  188. void tipc_named_node_up(u32 max_item_buf, u32 node)
  189. {
  190. LIST_HEAD(message_list);
  191. read_lock_bh(&tipc_nametbl_lock);
  192. named_distribute(&message_list, node, &publ_cluster, max_item_buf);
  193. named_distribute(&message_list, node, &publ_zone, max_item_buf);
  194. read_unlock_bh(&tipc_nametbl_lock);
  195. tipc_link_names_xmit(&message_list, node);
  196. }
  197. /**
  198. * named_purge_publ - remove publication associated with a failed node
  199. *
  200. * Invoked for each publication issued by a newly failed node.
  201. * Removes publication structure from name table & deletes it.
  202. */
  203. static void named_purge_publ(struct publication *publ)
  204. {
  205. struct publication *p;
  206. write_lock_bh(&tipc_nametbl_lock);
  207. p = tipc_nametbl_remove_publ(publ->type, publ->lower,
  208. publ->node, publ->ref, publ->key);
  209. if (p)
  210. tipc_nodesub_unsubscribe(&p->subscr);
  211. write_unlock_bh(&tipc_nametbl_lock);
  212. if (p != publ) {
  213. pr_err("Unable to remove publication from failed node\n"
  214. " (type=%u, lower=%u, node=0x%x, ref=%u, key=%u)\n",
  215. publ->type, publ->lower, publ->node, publ->ref,
  216. publ->key);
  217. }
  218. kfree(p);
  219. }
  220. /**
  221. * tipc_named_rcv - process name table update message sent by another node
  222. */
  223. void tipc_named_rcv(struct sk_buff *buf)
  224. {
  225. struct publication *publ;
  226. struct tipc_msg *msg = buf_msg(buf);
  227. struct distr_item *item = (struct distr_item *)msg_data(msg);
  228. u32 count = msg_data_sz(msg) / ITEM_SIZE;
  229. write_lock_bh(&tipc_nametbl_lock);
  230. while (count--) {
  231. if (msg_type(msg) == PUBLICATION) {
  232. publ = tipc_nametbl_insert_publ(ntohl(item->type),
  233. ntohl(item->lower),
  234. ntohl(item->upper),
  235. TIPC_CLUSTER_SCOPE,
  236. msg_orignode(msg),
  237. ntohl(item->ref),
  238. ntohl(item->key));
  239. if (publ) {
  240. tipc_nodesub_subscribe(&publ->subscr,
  241. msg_orignode(msg),
  242. publ,
  243. (net_ev_handler)
  244. named_purge_publ);
  245. }
  246. } else if (msg_type(msg) == WITHDRAWAL) {
  247. publ = tipc_nametbl_remove_publ(ntohl(item->type),
  248. ntohl(item->lower),
  249. msg_orignode(msg),
  250. ntohl(item->ref),
  251. ntohl(item->key));
  252. if (publ) {
  253. tipc_nodesub_unsubscribe(&publ->subscr);
  254. kfree(publ);
  255. } else {
  256. pr_err("Unable to remove publication by node 0x%x\n"
  257. " (type=%u, lower=%u, ref=%u, key=%u)\n",
  258. msg_orignode(msg), ntohl(item->type),
  259. ntohl(item->lower), ntohl(item->ref),
  260. ntohl(item->key));
  261. }
  262. } else {
  263. pr_warn("Unrecognized name table message received\n");
  264. }
  265. item++;
  266. }
  267. write_unlock_bh(&tipc_nametbl_lock);
  268. kfree_skb(buf);
  269. }
  270. /**
  271. * tipc_named_reinit - re-initialize local publications
  272. *
  273. * This routine is called whenever TIPC networking is enabled.
  274. * All name table entries published by this node are updated to reflect
  275. * the node's new network address.
  276. */
  277. void tipc_named_reinit(void)
  278. {
  279. struct publication *publ;
  280. int scope;
  281. write_lock_bh(&tipc_nametbl_lock);
  282. for (scope = TIPC_ZONE_SCOPE; scope <= TIPC_NODE_SCOPE; scope++)
  283. list_for_each_entry(publ, &publ_lists[scope]->list, local_list)
  284. publ->node = tipc_own_addr;
  285. write_unlock_bh(&tipc_nametbl_lock);
  286. }