name_distr.c 10 KB

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