name_distr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*
  2. * net/tipc/name_distr.c: TIPC name distribution code
  3. *
  4. * Copyright (c) 2000-2006, 2014, 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. int sysctl_tipc_named_timeout __read_mostly = 2000;
  40. /**
  41. * struct tipc_dist_queue - queue holding deferred name table updates
  42. */
  43. static struct list_head tipc_dist_queue = LIST_HEAD_INIT(tipc_dist_queue);
  44. struct distr_queue_item {
  45. struct distr_item i;
  46. u32 dtype;
  47. u32 node;
  48. unsigned long expires;
  49. struct list_head next;
  50. };
  51. /**
  52. * publ_to_item - add publication info to a publication message
  53. */
  54. static void publ_to_item(struct distr_item *i, struct publication *p)
  55. {
  56. i->type = htonl(p->type);
  57. i->lower = htonl(p->lower);
  58. i->upper = htonl(p->upper);
  59. i->ref = htonl(p->ref);
  60. i->key = htonl(p->key);
  61. }
  62. /**
  63. * named_prepare_buf - allocate & initialize a publication message
  64. */
  65. static struct sk_buff *named_prepare_buf(u32 type, u32 size, u32 dest)
  66. {
  67. struct sk_buff *buf = tipc_buf_acquire(INT_H_SIZE + size);
  68. struct tipc_msg *msg;
  69. if (buf != NULL) {
  70. msg = buf_msg(buf);
  71. tipc_msg_init(msg, NAME_DISTRIBUTOR, type, INT_H_SIZE, dest);
  72. msg_set_size(msg, INT_H_SIZE + size);
  73. }
  74. return buf;
  75. }
  76. void named_cluster_distribute(struct sk_buff *skb)
  77. {
  78. struct sk_buff *oskb;
  79. struct tipc_node *node;
  80. u32 dnode;
  81. rcu_read_lock();
  82. list_for_each_entry_rcu(node, &tipc_node_list, list) {
  83. dnode = node->addr;
  84. if (in_own_node(dnode))
  85. continue;
  86. if (!tipc_node_active_links(node))
  87. continue;
  88. oskb = skb_copy(skb, GFP_ATOMIC);
  89. if (!oskb)
  90. break;
  91. msg_set_destnode(buf_msg(oskb), dnode);
  92. tipc_link_xmit_skb(oskb, dnode, dnode);
  93. }
  94. rcu_read_unlock();
  95. kfree_skb(skb);
  96. }
  97. /**
  98. * tipc_named_publish - tell other nodes about a new publication by this node
  99. */
  100. struct sk_buff *tipc_named_publish(struct publication *publ)
  101. {
  102. struct sk_buff *buf;
  103. struct distr_item *item;
  104. list_add_tail_rcu(&publ->local_list,
  105. &tipc_nametbl->publ_list[publ->scope]);
  106. if (publ->scope == TIPC_NODE_SCOPE)
  107. return NULL;
  108. buf = named_prepare_buf(PUBLICATION, ITEM_SIZE, 0);
  109. if (!buf) {
  110. pr_warn("Publication distribution failure\n");
  111. return NULL;
  112. }
  113. item = (struct distr_item *)msg_data(buf_msg(buf));
  114. publ_to_item(item, publ);
  115. return buf;
  116. }
  117. /**
  118. * tipc_named_withdraw - tell other nodes about a withdrawn publication by this node
  119. */
  120. struct sk_buff *tipc_named_withdraw(struct publication *publ)
  121. {
  122. struct sk_buff *buf;
  123. struct distr_item *item;
  124. list_del(&publ->local_list);
  125. if (publ->scope == TIPC_NODE_SCOPE)
  126. return NULL;
  127. buf = named_prepare_buf(WITHDRAWAL, ITEM_SIZE, 0);
  128. if (!buf) {
  129. pr_warn("Withdrawal distribution failure\n");
  130. return NULL;
  131. }
  132. item = (struct distr_item *)msg_data(buf_msg(buf));
  133. publ_to_item(item, publ);
  134. return buf;
  135. }
  136. /**
  137. * named_distribute - prepare name info for bulk distribution to another node
  138. * @list: list of messages (buffers) to be returned from this function
  139. * @dnode: node to be updated
  140. * @pls: linked list of publication items to be packed into buffer chain
  141. */
  142. static void named_distribute(struct sk_buff_head *list, u32 dnode,
  143. struct list_head *pls)
  144. {
  145. struct publication *publ;
  146. struct sk_buff *skb = NULL;
  147. struct distr_item *item = NULL;
  148. uint msg_dsz = (tipc_node_get_mtu(dnode, 0) / ITEM_SIZE) * ITEM_SIZE;
  149. uint msg_rem = msg_dsz;
  150. list_for_each_entry(publ, pls, local_list) {
  151. /* Prepare next buffer: */
  152. if (!skb) {
  153. skb = named_prepare_buf(PUBLICATION, msg_rem, dnode);
  154. if (!skb) {
  155. pr_warn("Bulk publication failure\n");
  156. return;
  157. }
  158. item = (struct distr_item *)msg_data(buf_msg(skb));
  159. }
  160. /* Pack publication into message: */
  161. publ_to_item(item, publ);
  162. item++;
  163. msg_rem -= ITEM_SIZE;
  164. /* Append full buffer to list: */
  165. if (!msg_rem) {
  166. __skb_queue_tail(list, skb);
  167. skb = NULL;
  168. msg_rem = msg_dsz;
  169. }
  170. }
  171. if (skb) {
  172. msg_set_size(buf_msg(skb), INT_H_SIZE + (msg_dsz - msg_rem));
  173. skb_trim(skb, INT_H_SIZE + (msg_dsz - msg_rem));
  174. __skb_queue_tail(list, skb);
  175. }
  176. }
  177. /**
  178. * tipc_named_node_up - tell specified node about all publications by this node
  179. */
  180. void tipc_named_node_up(u32 dnode)
  181. {
  182. struct sk_buff_head head;
  183. __skb_queue_head_init(&head);
  184. rcu_read_lock();
  185. named_distribute(&head, dnode,
  186. &tipc_nametbl->publ_list[TIPC_CLUSTER_SCOPE]);
  187. named_distribute(&head, dnode,
  188. &tipc_nametbl->publ_list[TIPC_ZONE_SCOPE]);
  189. rcu_read_unlock();
  190. tipc_link_xmit(&head, dnode, dnode);
  191. }
  192. static void tipc_publ_subscribe(struct publication *publ, u32 addr)
  193. {
  194. struct tipc_node *node;
  195. if (in_own_node(addr))
  196. return;
  197. node = tipc_node_find(addr);
  198. if (!node) {
  199. pr_warn("Node subscription rejected, unknown node 0x%x\n",
  200. addr);
  201. return;
  202. }
  203. tipc_node_lock(node);
  204. list_add_tail(&publ->nodesub_list, &node->publ_list);
  205. tipc_node_unlock(node);
  206. }
  207. static void tipc_publ_unsubscribe(struct publication *publ, u32 addr)
  208. {
  209. struct tipc_node *node;
  210. node = tipc_node_find(addr);
  211. if (!node)
  212. return;
  213. tipc_node_lock(node);
  214. list_del_init(&publ->nodesub_list);
  215. tipc_node_unlock(node);
  216. }
  217. /**
  218. * tipc_publ_purge - remove publication associated with a failed node
  219. *
  220. * Invoked for each publication issued by a newly failed node.
  221. * Removes publication structure from name table & deletes it.
  222. */
  223. static void tipc_publ_purge(struct publication *publ, u32 addr)
  224. {
  225. struct publication *p;
  226. spin_lock_bh(&tipc_nametbl_lock);
  227. p = tipc_nametbl_remove_publ(publ->type, publ->lower,
  228. publ->node, publ->ref, publ->key);
  229. if (p)
  230. tipc_publ_unsubscribe(p, addr);
  231. spin_unlock_bh(&tipc_nametbl_lock);
  232. if (p != publ) {
  233. pr_err("Unable to remove publication from failed node\n"
  234. " (type=%u, lower=%u, node=0x%x, ref=%u, key=%u)\n",
  235. publ->type, publ->lower, publ->node, publ->ref,
  236. publ->key);
  237. }
  238. kfree_rcu(p, rcu);
  239. }
  240. void tipc_publ_notify(struct list_head *nsub_list, u32 addr)
  241. {
  242. struct publication *publ, *tmp;
  243. list_for_each_entry_safe(publ, tmp, nsub_list, nodesub_list)
  244. tipc_publ_purge(publ, addr);
  245. }
  246. /**
  247. * tipc_update_nametbl - try to process a nametable update and notify
  248. * subscribers
  249. *
  250. * tipc_nametbl_lock must be held.
  251. * Returns the publication item if successful, otherwise NULL.
  252. */
  253. static bool tipc_update_nametbl(struct distr_item *i, u32 node, u32 dtype)
  254. {
  255. struct publication *publ = NULL;
  256. if (dtype == PUBLICATION) {
  257. publ = tipc_nametbl_insert_publ(ntohl(i->type), ntohl(i->lower),
  258. ntohl(i->upper),
  259. TIPC_CLUSTER_SCOPE, node,
  260. ntohl(i->ref), ntohl(i->key));
  261. if (publ) {
  262. tipc_publ_subscribe(publ, node);
  263. return true;
  264. }
  265. } else if (dtype == WITHDRAWAL) {
  266. publ = tipc_nametbl_remove_publ(ntohl(i->type), ntohl(i->lower),
  267. node, ntohl(i->ref),
  268. ntohl(i->key));
  269. if (publ) {
  270. tipc_publ_unsubscribe(publ, node);
  271. kfree_rcu(publ, rcu);
  272. return true;
  273. }
  274. } else {
  275. pr_warn("Unrecognized name table message received\n");
  276. }
  277. return false;
  278. }
  279. /**
  280. * tipc_named_add_backlog - add a failed name table update to the backlog
  281. *
  282. */
  283. static void tipc_named_add_backlog(struct distr_item *i, u32 type, u32 node)
  284. {
  285. struct distr_queue_item *e;
  286. unsigned long now = get_jiffies_64();
  287. e = kzalloc(sizeof(*e), GFP_ATOMIC);
  288. if (!e)
  289. return;
  290. e->dtype = type;
  291. e->node = node;
  292. e->expires = now + msecs_to_jiffies(sysctl_tipc_named_timeout);
  293. memcpy(e, i, sizeof(*i));
  294. list_add_tail(&e->next, &tipc_dist_queue);
  295. }
  296. /**
  297. * tipc_named_process_backlog - try to process any pending name table updates
  298. * from the network.
  299. */
  300. void tipc_named_process_backlog(void)
  301. {
  302. struct distr_queue_item *e, *tmp;
  303. char addr[16];
  304. unsigned long now = get_jiffies_64();
  305. list_for_each_entry_safe(e, tmp, &tipc_dist_queue, next) {
  306. if (time_after(e->expires, now)) {
  307. if (!tipc_update_nametbl(&e->i, e->node, e->dtype))
  308. continue;
  309. } else {
  310. tipc_addr_string_fill(addr, e->node);
  311. pr_warn_ratelimited("Dropping name table update (%d) of {%u, %u, %u} from %s key=%u\n",
  312. e->dtype, ntohl(e->i.type),
  313. ntohl(e->i.lower),
  314. ntohl(e->i.upper),
  315. addr, ntohl(e->i.key));
  316. }
  317. list_del(&e->next);
  318. kfree(e);
  319. }
  320. }
  321. /**
  322. * tipc_named_rcv - process name table update message sent by another node
  323. */
  324. void tipc_named_rcv(struct sk_buff *buf)
  325. {
  326. struct tipc_msg *msg = buf_msg(buf);
  327. struct distr_item *item = (struct distr_item *)msg_data(msg);
  328. u32 count = msg_data_sz(msg) / ITEM_SIZE;
  329. u32 node = msg_orignode(msg);
  330. spin_lock_bh(&tipc_nametbl_lock);
  331. while (count--) {
  332. if (!tipc_update_nametbl(item, node, msg_type(msg)))
  333. tipc_named_add_backlog(item, msg_type(msg), node);
  334. item++;
  335. }
  336. tipc_named_process_backlog();
  337. spin_unlock_bh(&tipc_nametbl_lock);
  338. kfree_skb(buf);
  339. }
  340. /**
  341. * tipc_named_reinit - re-initialize local publications
  342. *
  343. * This routine is called whenever TIPC networking is enabled.
  344. * All name table entries published by this node are updated to reflect
  345. * the node's new network address.
  346. */
  347. void tipc_named_reinit(void)
  348. {
  349. struct publication *publ;
  350. int scope;
  351. spin_lock_bh(&tipc_nametbl_lock);
  352. for (scope = TIPC_ZONE_SCOPE; scope <= TIPC_NODE_SCOPE; scope++)
  353. list_for_each_entry_rcu(publ, &tipc_nametbl->publ_list[scope],
  354. local_list)
  355. publ->node = tipc_own_addr;
  356. spin_unlock_bh(&tipc_nametbl_lock);
  357. }